Jump to content

ArrayList help

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Ritwik I the programmer

Ritwik I the programmer

    Newbie

  • Members
  • PipPip
  • 17 posts
Could anyone explain to me how arraylist works? I thought I should ask to get better explanations before I consult Oracle.
(Note:I know about too few classes associated with this subject.)

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
How to use ArrayList in Java ?
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
Ritwik I the programmer

Ritwik I the programmer

    Newbie

  • Members
  • PipPip
  • 17 posts
I'm checking out that link on your suggestion. But could anyone explain how ArrayList<Object> works? I've used it in a school project a know its eneral purpose, but would like to know how it works.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Normal declaration:
ArrayList myList = new ArrayList();
This list can hold anything:
myList.add(new String(""));
myList.add(new Integer(1);
myList.add(true);
All of this will work.

By saying ArrayList<Object> you state that the list can only hold elements of type Object.
Now, because everything in Java always extends the Object class, you can also put anything you want in it. The following will work with no problem:
ArrayList<Object> myList = new ArrayList<Object>();
myList.add(new String(""));
myList.add(new Integer(1);
myList.add(true);

So there is no actual difference between saying ArrayList or ArrayList<Object>.
Note you can't store primitives into an ArrayList(int, double, float, short, byte, boolean, long, char).
If you want to put those into the list, you need to use their wrapper class (Integer, Double, Float, Short, Byte, Boolean, Long, Character)
This however works:
ArrayList<Integer> myList = new ArrayList<Integer>();
int i=1;
myList.add(i);

Quote

What? You just said you can't put primitives into an ArrayList.
Correct, but when you compile this, the compiler will make sure the int is transformed into an Integer properly. This is called "autoboxing" (more info: Autoboxing)

You should also know that this restriction of what you can put in an ArrayList (or any other Collection) is only checked at compile-time, and not at runtime.
You write down
ArrayList<Integer> myList = new ArrayList<Integer>();
After compilation, and what Java will actually be running is:
ArrayList myList = new ArrayList();
This! The JVM has no idea about the restrictions, it's just a List where you can put anything in.

So even though you declare an ArrayList to, say, only contain Strings. You can still dump other stuff in it if you want to:
public static void main(String[] args){
    ArrayList<String> myStrings = new ArrayList<String>();
    myMethod(myStrings);
    for(String word : myStrings){
        System.out.println(word);
    }
}

public static void myMethod(List myList){ //Compiler can't see what kind of List will be send here. At runtime Java doesn't notice there's an "ArrayList<String>" here, because at runtime only "ArrayList" remains.
    myList.add(1);
    myList.add(true);
}
This example compiles with no problem (possibly a warning, but it will compile). But this definitely crashes at runtime.

If this myMethod was written with the generics, this wouldn't have compiled:
public static void myMethod(List<String> myList){ 
    myList.add(1);  //Compiler sees this can't be put in that List -> compilation fails.
    myList.add(true);
}
So always try to use generics to limit your collections. And when doing so, try to be as specific as possible.
By "be as specific as possible" I mean, if you know the list will only contain Integer elements, then also declare it that way:
List<Integer> goodList = new ArrayList<Integer>();
List<Number> somewhatGoodList = new ArrayList<Number>();
List<Object> badList = new ArrayList<Object>();
(Integer extends Number, but so do BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short. So you're not 100% your list will contain Integers.)

#5
Ritwik I the programmer

Ritwik I the programmer

    Newbie

  • Members
  • PipPip
  • 17 posts
Thanks, wim DC. It helped a lot. But I had just given the syntactical type in ArrayList<Object>, not actually how to use ArrayList for Object class.

#6
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Are you looking for the logic behind the ArrayList?
It contains an array of Object, which is redeclared as a bigger array if it gets filled and all the elements get copied to a new array.

int newCapacity = (oldCapacity * 3)/2 + 1;





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users