Page 1 of 1

About Question enthuware.ocajp.i.v7.2.850 :

Posted: Mon Dec 02, 2013 4:41 pm
by javaman
You must specify the class of objects you want to store in ArrayList when you declare a variable of type ArrayList.
This is not true because you can still use non-generic form. For example, instead of using ArrayList<String> listOfStrings; you can use: ArrayList listOfStrings; Of course, if you use non generic version, you will lose the compile time type checking.

Is this illustrated b y the following

import java.util.ArrayList;

class LearnJava{
public static void main(String args[]){
ArrayList<String> al = new ArrayList<>();
al.add("0");
a1.add(1);
ArrayList al2 = new ArrayList();
al2.add("0");
al2.add(1);
for(Object i : al2){
System.out.println(i);
}
}
}

a1.add(1); gives a compile error because I declared the ArrayList to contain Strings and 1 is an integer. Compiler is making sure I add what I specified.
Since I do not specify the type of element al2 can contain I have to use a loopvariable of type Object in for(Object i : al2) and therefore lose the item-specific functions since loopvariable i is of type Object instead of e.g. String?

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Mon Dec 02, 2013 7:52 pm
by admin
You can only store objects (not primitives) in an ArrayList that is why add(1); fails to compile. The rest is correct i.e. you can "use a loopvariable of type Object in for(Object i : al2) and therefore lose the item-specific functions since loopvariable i is of type Object instead of e.g. String"

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Wed Jul 29, 2015 5:39 pm
by Vermeulen
admin wrote:You can only store objects (not primitives) in an ArrayList that is why add(1); fails to compile.
True but due to autoboxing, add(1); compiles fine for ArrayLists that can contain Integers, i.e. a raw ArrayList, ArrayList<Object>, ArrayList<Number>, and ArrayList<Integer>.

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Fri Aug 07, 2015 2:18 pm
by sir_Anduin@yahoo.de
Hi,

where does the method .sort() comes from.
I have checked the collection interface doc, but there is no such method:

http://docs.oracle.com/javase/7/docs/ap ... ction.html

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Fri Aug 07, 2015 11:34 pm
by admin
The sort method is in Collections class not Collection interface.

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Sat Aug 08, 2015 3:49 am
by sir_Anduin@yahoo.de
thanks :)

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Fri May 03, 2019 11:41 am
by flex567
Collections class is never mentioned in the book. Do we need to know anything about it for the exam?

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Fri May 03, 2019 9:01 pm
by admin
No, Collections class is not required for the exam. But some candidates have reported seeing it mentioned in the options in relation to sorting an array list. So, it will be good if you know that Collections.sort can be used to sort an ArrayList. Nothing more is required.

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Wed Jul 08, 2020 3:40 am
by Dreamweaver
It allows you to access its elements in random order.
This question is very ambiguity because get(index) method return the element at a specified position (index) in the list not random

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Wed Jul 08, 2020 5:00 am
by admin
No, "ability to access elements in random order" is a standard industry/computer science term. It is well understood everywhere.
See this discussion: https://stackoverflow.com/questions/327 ... -behaviour

Re: About Question enthuware.ocajp.i.v7.2.850 :

Posted: Wed Jul 08, 2020 3:49 pm
by Dreamweaver
ok, Thank you for explanation.