• Post Reply Bookmark Topic Watch Topic
  • New Topic

query about improved dot com class in chapter6 head first java  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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??
 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



but ArrayList <String> locationCells=new ArrayList<String> ...why dont use this format??? similar to object ArrayList???
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



is Arrays.asList() Works same as ArrayList??

One mor equery is instead of creating the locations as int[] locations and then converting it into list ,can we create directlY an ArrayList of locations,So that we can pass the parameters directly as ArrayList through methods.???I read somewhere in the book that we cant create ArrayList of Primitives or integer variables ...so in that case what should be done???
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot create Lists of primitives. But you can create a List<Integer> and use boxing
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??




I would like to add one more query here..i,e.. //here why cant we write directly locationcells.remove(guess) just like we can do for objects like ,myList.remove(s), where s is object reference and String is also an object.So why not???


please check the query inside the code..
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, you cannot create Lists of primitives. But you can create a List<Integer> and use boxing



Thanks
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't put such queries inside code; they make the lines so long they cannot be read. I had to move it outside the code.

What is the difference between those two ways to create a List? I can't see a difference?
If you have a List<Integer> or a List<String>, have you tried remove()? I can see no reason why it should not work. But I haven't studies the dot‑com game for a long time. You need to be sure that you are using the correct version of the remove() method. If you pass an int, you will get one version and if you pass an Integer I think you will get the other version. You will have to try it and see. Try overriding those methods. This is how you can do it with an anonymous class:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!