My first query is :when we create an arraylist for objects we have a different way to declare it as given in Page no 133 i;e ArrayList<Egg> myList = new ArrayList<Egg> () ;
but in this class creating a String arrayList we used different format i,e Private Arraylist<String> locationCells....Why ???
All things are lawful, but not all things are profitable.
Second question is :Whatever modification we made in this dotcom class i,e public void setLocationCells(Arraylist<String> locate) ..In this method method variable is declared as arraylist but in Simpledotcomgame class
the locations are declared as int [] array i,e
int[] locations={random,random+2,random+3};dot.setLocationCells(locations);
...So in the source class do i need to change the type of the locations???...How??
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
My first query is :when we create an arraylist for objects we have a different way to declare it as given in Page no 133 i;e ArrayList<Egg> myList = new ArrayList<Egg> () ;
but in this class creating a String arrayList we used different format i,e Private Arraylist<String> locationCells....Why ???
ArrayList is a generic type, meaning, you the programmer should tell it what type of list it is. So ArrayList<Egg> is a list of eggs and ArrayList<String> is a list of Strings.
Knute Snortum wrote:
Second question is :Whatever modification we made in this dotcom class i,e public void setLocationCells(Arraylist<String> locate) ..In this method method variable is declared as arraylist but in Simpledotcomgame class
the locations are declared as int [] array i,e
int[] locations={random,random+2,random+3};dot.setLocationCells(locations);
...So in the source class do i need to change the type of the locations???...How??
You have a field int[] locations that is set by a method with this signature: setLocationCells(Arraylist<String> locate)? That's really bad design.
How about changing locations to a List? Then you can use Arrays.asList() to turn an array into a list.
Nabanita Sarkar wrote:
this is my improved code of dotcom class..
My first query is :when we create an arraylist for objects we have a different way to declare it as given in Page no 133 i;e ArrayList<Egg> myList = new ArrayList<Egg> () ;
but in this class creating a String arrayList we used different format i,e Private Arraylist<String> locationCells....Why ???
Second question is :Whatever modification we made in this dotcom class i,e public void setLocationCells(Arraylist<String> locate) ..In this method method variable is declared as arraylist but in Simpledotcomgame class
the locations are declared as int [] array i,e ...So in the source class do i need to change the type of the locations???...How??
Can anyone help me out to solve these??
Campbell Ritchie wrote:No, you cannot create Lists of primitives. But you can create a List<Integer> and use boxing