Results 1 to 4 of 4
Thread: Array and arraylist
- 11-28-2009, 04:06 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Array and arraylist
1. I don't really know the difference btw array and array list. Their definitions are:An array list is a sequence of of objects.
An array is a fixed-length sequence of values of the same type, which can be an object type or a primitive type.
I still can't quite distinguish them. Is that an array has elements of specified type, whereas an array list holds a collection of Object references??
2. Is the following the declaration of array:
int[] data=new int[20];
Is this the same as the above: String[] s={"011","101","110","010","001"}; but why here it doesn't say: String[] s=new string[5]? are they the same thing?
3. Is the following declaration of an arraylist?
ArrayList coins=new ArrayList();
THANKS!!!
- 11-28-2009, 04:28 AM #2
There are a number of key differences.
First off, you can say
This will create an empty array that can hold 5 Strings.Java Code:String[] s = new String[5];
Second, in your second question:
are not the same thing. Both are arrays of Strings, but in the first case, you are creating an empty array of size 20, and in the second case you are creating and assigning (initializing) in one step to create a full array of size 5.Java Code:int[] data=new int[20]; String[] s={"011","101","110","010","001"};
The primary difference between array and ArrayList besides the fact that one is a language feature and the other is a data structure coded with java is that an ArrayList is dynamically expanding and an array is not.
Secondly, while usually not a good idea, ArrayLists can contain objects of different types. Since ArrayList is a class itself, it has a number of methods associated with it which arrays do not. Behind the scenes, ArrayList uses an array to hold data, but it provides means of expanding that array on the fly, removing elements and shifting, provides iterators, can perform basic searching (contains()), and insertion. Arrays do not have any of these features.
If you are creating static lists that do not change or mutable lists which have a fixed length, then often arrays are your best bet since they are small, fast and efficient.
If you are dealing with an unknown quantity of data, or want to avoid the intricacies of element shifting during removal and insertion, then ArrayList is the best bet. I use ArrayList for most of the general programing I do due to its dynamic nature.
Finally,
is valid. I suggest looking at the java api http://java.sun.com/javase/6/docs/api/ and reading up on ArrayList's methods.Java Code:ArrayList coins=new ArrayList();
- 11-29-2009, 02:11 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Thanks for your reply. Can you give me examples according to their definitions:
An array list is a sequence of of objects. What's one example?
An array is a fixed-length sequence of values of the same type, which can be an object type or a primitive type. What's one object type example?
- 11-29-2009, 02:22 AM #4
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
you can add to arraylists, but have to set the information in array so for example
now theres no fixed amount. so you can doJava Code:java.util.ArrayList<String> list = new java.util.ArrayList<String>();
as many times as you wish.Java Code:list.add("");
but with arrays, you have to set the amount or set the data before hand. so for example
etc...or you could doJava Code:String[] list = new String[5](); list[0] = ""; list[1] = "";
examples of arraylists is there isnt a fixed amount of data storage in themJava Code:String[] list = {"", "", "", "", ""}();
examples of arrays is you can make them 2dimensional. so for example
orJava Code:String[][] list = {{"", ""}{"", ""}{"", ""}}();
hope you learn from this.Java Code:String[][] list = new String[][](); list[0][0] = ""; list[0][1] = ""; list[0][2] = "";
Similar Threads
-
Need to change an ArrayList to an Array
By trojansc82 in forum New To JavaReplies: 9Last Post: 07-25-2010, 08:26 PM -
Copying ArrayList into an Array
By Manfizy in forum New To JavaReplies: 6Last Post: 07-16-2009, 07:03 AM -
Converting ArrayList to Array
By vasavi.singh in forum New To JavaReplies: 1Last Post: 02-23-2009, 02:34 PM -
Converting ArrayList to Array
By Java Tip in forum Java TipReplies: 0Last Post: 11-13-2007, 10:41 AM -
Array to ArrayList
By javaplus in forum New To JavaReplies: 2Last Post: 11-12-2007, 12:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks