• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Quick Question About This Method In Java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!
Just curious about the following code:


If I use a for each of loop on an arraylist to call this method three times, each time with a different object, that has a different location, and I use it to set the locations for each object, will it create a new loc arraylist for each object or just set them all into one arraylist?

Thanks!
 
Saloon Keeper
Posts: 7355
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code does not create any objects, it merely stores a reference to an object.
 
Marshal
Posts: 75842
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is also inherently unsafe because you are creating an alias to the List from elsewhere.
 
Daniel Maguire
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so I got this code from Head First Java, in the section where they make the dotcom battle ships game - the more advanced version, and I'm trying to understand how it works.
I'll post the relevant code (at least what I think is relevant with very little experience in programming) and hopefully someone can help me understand?







Now the thing I'm confused about with this code is that there needs to be a different arrayList for each dotcom in the check yourself method, because if a dotcom is sunk it needs to print "kill", however if there all stored in one ArrayList, it will only print "kill" when ALL dotcoms are sunk from my understanding. The dotComs unique location arraylist needs to be used in the "checkyourself" method, which currently uses the locationCells arraylist I believe. I'm just wondering, does this code use a unique arraylist for each dotcom object in the check yourself method, and if not, how would it work to print "kill" when EACH dotCom is sunk?

P.S.: This forum is great, you're all so helpful and quick to respond!
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each dotComsList has its own ArrayList of locationCells to check.  For example, if the first dotComsList has d1, d2, and d3 those would be stored in the locationCells list for that object.  Then for the second dotComsList maybe it has a1, a2, a3 in the locationCells list.  

Next when the checkUserGuess method runs, it loops through each dotComsList object and checks the locationCells list within it.  So it will start with the first dotComsList object and then run checkYourself on that.  When it runs checkYourself, the code looks in the locationCells list which in my example contains d1, d2, and d3.  The code looks for the userInput within the locationCells list and if it exists in the list then the item is removed and either 'kill' or 'hit' is returned.  When the last value is found/removed from the list 'kill' is returned.

After it finishes with the first dotComsList, it looks at the next dotComsList (a1, a2, and a3).  And this continues through the same process looking for values in the locationCells.

Here's another way to explain it visually.
dotComsList(1)
  • locationCells(d1)
  • locationCells(d2)
  • locationCells(d3)

  • dotComsList(2)
  • locationCells(a1)
  • locationCells(a2)
  • locationCells(a3)
  •  
    reply
      Bookmark Topic Watch Topic
    • New Topic