About Question enthuware.ocajp.i.v7.2.850 :
Moderator: admin
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.850 :
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?
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Jul 15, 2015 4:05 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
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>.admin wrote:You can only store objects (not primitives) in an ArrayList that is why add(1); fails to compile.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
The sort method is in Collections class not Collection interface.
If you like our products and services, please help us by posting your review here.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
Collections class is never mentioned in the book. Do we need to know anything about it for the exam?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
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
This question is very ambiguity because get(index) method return the element at a specified position (index) in the list not random
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
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
See this discussion: https://stackoverflow.com/questions/327 ... -behaviour
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.850 :
ok, Thank you for explanation.
Who is online
Users browsing this forum: No registered users and 123 guests