ArrayList VS an ordinary Array?

Could someone explain to me (like I'm a 4-year-old) what the difference between an ArrayList and an ordinary array is?

I know how to use an Array (not to sure how to explain it though) but I'm a little lost with how an ArrayList works and what it can be used for that an ordinary array can't.

Also I'm given this code: https://gist.github.com/anonymous/7499236 for an exercise where I'm asked the following:

"Why are we looping backwards in the Programming Exercise?"

I don't even understand the question, are we looping backwards? Could someone also please explain this to me, because I don't get it at all :(

Thanks in advance!

Answers

  • line 10:

    for (int i = balls.size()-1; i >= 0; i--) {

    this is starting at the high number (balls.size() - 1) and working it's way to the low number (0) and the third clause, usually an increment, is actually a decrement...

    arrays are usually of a known size. they don't grow or shrink. ArrayLists can grow and shrink. you can access both in the same way, get at an item at a know position (myArray[10] or myArrayList.get(10), for instance). but with an ArrayList you can add things anywhere, remove them from anywhere and the remaining elements will shift up or down to make space. when you remove the 10th element, say, the 11th element becomes the new 10th element and so on until the end of the ArrayList.

    and that's why you've iterating through the list backwards - if you delete the 10th element (and the 11th etc shift up one) then the next time through the loop your attempts at getting the 11th element will skip the old 11th element (now 10th) and fetch the new 11th element (previously the 12th). deleting from the end forwards avoids this - it doesn't matter that everything behind the current position shifts up because you've already handled those.

  • edited November 2013

    The main difference between an array and an arraylist is that the size of the array must be defined before we use it examples -

    int[] a1 = new int[10]; // create a 10 element array

    int[] a2 = {3, -29, 34, 0, 12}; // create 5 element array and initialise the element values

    Looping (or more technically iterating) over an array means visiting each element of the array once. The safe way to do it is using the length of the array to prevent index-out-of-bounds errors. Example

    for(int index = 0; index < a2.length; index++){
      println(a2[index]);
    }
    

    This is called forward iteration (looping) because we move from the start to the end of the array. Reverse iteration involves moving from the end to the start of the array e.g.

    for(int index = a2.length - 1; index >= 0; index--){
      println(a2[index]);
    }
    

    The ArrayLIst is different in two ways

    1) It cannot hold primitive data types - must be objects

    2) that we don't have to define the size before we use it

    Although some examples appear to show ArrayList(s) holding primitive values they are in fact wrapped up in reference (object) types e.g. int >> Integer, float >> Float, boolean >> Boolean etc.

    // Create an empty array list
    ArrayList<Integer> al1 = new ArrayList<Integer>();
    
    // Add some integers (will be stored as Integer types)
    al1.add(3);  // now has 1 element
    al1.add(-29);  // now has 2 elements
    al1.add(34);  // now has 3 elements
    al1.add(0);  // now has 4 elements
    al1.add(12);  // now has 5 elements
    
    // Forward iteration
    for(int index = 0; index < al1.size(); index++){
      println(al1.get(index));
    }
    
    // Reverse iteration
    for(int index = al1.size()-1; index >= 0; index--){
      println(al1.get(index));
    }
    

    It is also possible to forward iterate with the for-each loop. This shows it in action with the ArrayList. Simple change al1 with the name of an int-array to work with arrays

    for(Integer n : al1){
      println(n);
    }
    

    HTH

  • Thanks! you guys are awesome <3

    uhm... what is an "element" considered as in processing?

  • An element is a way of describing a position inside an array or ArrayList. So in

    float[] n = new float[69];

    n can be described as an array that can hold 69 float values or an array with 69 elements

  • edited November 2013

    A more compact way of writing backwards for loops: o->

    for (int i = myArr.length; i-- != 0;)   println(myArr[i]);
    

    And w/ while loop:

    int i = myArr.length;
    while (i-- != 0)   println(myArr[i]);
    
Sign In or Register to comment.