array or array list?
I am trying to write a program that takes a series of integers and puts them into an array. Now i thought that that meant i had to put them into an array list, but it crashes. More specifically im trying to take a series of integers from input and alternate between signs and output an aswer
E.G input- 3,4,5,6,7,8,9,10,9,1
Whats the difference between an array and array list
an array has a set size, and that size cannot be altered without reconstructing the array. An ArrayList can expand dynamically(without being reconstructed) using the add(E) method. ArrayLists use Generics, and should be parameterized. However, Generics does not allow primitives such as int, but wrapper classes such as Integer may be used.
arraylist is implemented using arrays. the two are the same, just that arraylist has several useful things implemented for you already, but as boyo mentioned, you need wrapper classes for primitives like ints in an arraylist.
arraylist is dynamic array..the size of arraylist is decided at run time...arrays have fixed size
if you dont know the number of inputs that user enter, you better use ArrayList<Integer>
review of array concepts
An array is addressed like a checker board by row and column.
A list has pointers that take you from one element to the next . i.e. you are at 1,1, go to square 5,4 for the 2nd element. The benefit about a list is the ease of inserting new items, you don't have to copy the entire data to new locations. You just change the pointers. i.e, your are at 1,1, go to 4,5, then to 5,4. Don't want to get to deep into basic com sci. You can find more about pointers and lists from other sources, particularly in C/C++ sources.
Ultimately, any computer memory is an array. The whole point about interfaces is that they don't change. You can design an interface to look like arrays using lists as an implementation.
I could be wrong about the actual Java implementations; this is purely abstract, but an array implemented as a physical array in memory is addressed with fixed locations and traversing an array using takes you from square one to square two simply by incrementing the address. Using an array list, somearray might be at locatom 0 in memory, somearray might be at location 5, etc. There are tradeoffs in ease of insertion vs speed of addressing between the two methods.
Even a two (or more) dimension array has to have the address calculated from a linear address scheme.