Results 1 to 4 of 4
  1. #1
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default 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!!!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    There are a number of key differences.

    First off, you can say
    Java Code:
    String[] s = new String[5];
    This will create an empty array that can hold 5 Strings.

    Second, in your second question:
    Java Code:
    int[] data=new int[20];
    String[] s={"011","101","110","010","001"};
    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.

    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,
    Java Code:
    ArrayList coins=new ArrayList();
    is valid. I suggest looking at the java api http://java.sun.com/javase/6/docs/api/ and reading up on ArrayList's methods.

  3. #3
    MIA6 is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    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?

  4. #4
    Rock-On is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    you can add to arraylists, but have to set the information in array so for example

    Java Code:
    java.util.ArrayList<String> list = new java.util.ArrayList<String>();
    now theres no fixed amount. so you can do

    Java Code:
    list.add("");
    as many times as you wish.

    but with arrays, you have to set the amount or set the data before hand. so for example


    Java Code:
    String[] list = new String[5]();
    list[0] = "";
    list[1] = "";
    etc...or you could do

    Java Code:
    String[] list = {"", "", "", "", ""}();
    examples of arraylists is there isnt a fixed amount of data storage in them
    examples of arrays is you can make them 2dimensional. so for example

    Java Code:
    String[][] list = {{"", ""}{"", ""}{"", ""}}();
    or
    Java Code:
    String[][] list = new String[][]();
    list[0][0] = "";
    list[0][1] = "";
    list[0][2] = "";
    hope you learn from this.

Similar Threads

  1. Need to change an ArrayList to an Array
    By trojansc82 in forum New To Java
    Replies: 9
    Last Post: 07-25-2010, 09:26 PM
  2. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 08:03 AM
  3. Converting ArrayList to Array
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 03:34 PM
  4. Converting ArrayList to Array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 11:41 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •