Results 1 to 6 of 6
  1. #1
    Drukqs is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default 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
    computation- 3-4+5-6+7-8+9-10+9-1
    output- 4
    Whats the difference between an array and array list

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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.

  4. #4
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    arraylist is dynamic array..the size of arraylist is decided at run time...arrays have fixed size

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if you dont know the number of inputs that user enter, you better use ArrayList<Integer>

  6. #6
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default 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[0] might be at locatom 0 in memory, somearray[1] 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.

Similar Threads

  1. linked list or array?
    By sick_peng in forum New To Java
    Replies: 6
    Last Post: 04-15-2009, 07:33 PM
  2. Array List
    By mprentice84 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 02:12 AM
  3. array list help
    By dorno83 in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 09:56 AM
  4. Array List problem
    By kurenai in forum New To Java
    Replies: 2
    Last Post: 06-25-2008, 07:30 AM
  5. using an Array list
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 09:08 PM

Posting Permissions

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