This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes storing and displaying 2d arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "storing and displaying 2d arrays" Watch "storing and displaying 2d arrays" New topic
Author

storing and displaying 2d arrays

akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
Hello,

i have a small doubt . if i have 5 different animals and each one has 3 weapons. these animals and weapons are already in a arraylist created. I want to display like shown below. But the weapons are to be selected for each animal randomly.

How to store them as a 2d array ? or which is the alternate way to store them so as to display them like a table using formatter methods.

Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
You could define two classes, Animal and Weapon, make a collection of weapons be a field in Animal and have an array of Animals. When you iterate over the animals, you just get its collection of weapons to display.
akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
Emanuel Kadziela wrote:You could define two classes, Animal and Weapon, make a collection of weapons be a field in Animal and have an array of Animals. When you iterate over the animals, you just get its collection of weapons to display.


oh wow i will try this and let you know how is it working
Mikael Saltzman
Greenhorn

Joined: Dec 07, 2012
Posts: 9
Would a hashmap of type be of any use perhaps? You could then store the animals in the key string and the three weapons randomly selected in array of strings value; or replace the key string and the value string array with customized objects and get the stringst through an overridden toString(). Just a thought...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Mikael Saltzman wrote:Would a hashmap of type be of any use perhaps? . . .
Welcome to the Ranch (again).

We have a very new FAQ, not even a week old, about strings.
Surely you mean Map<Animal, Weapon[]> or similar. Also declare the reference as type Map Map<Foo, Bar> mapping = new HashMap<>();
The empty <> bit only works in Java7.
akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
Emanuel Kadziela wrote:You could define two classes, Animal and Weapon, make a collection of weapons be a field in Animal and have an array of Animals. When you iterate over the animals, you just get its collection of weapons to display.


I have been trying this since last night but dint get any Can you provide me a basic code struct for this please?
Mikael Saltzman
Greenhorn

Joined: Dec 07, 2012
Posts: 9
Alright, this is a bit messy, but anyway:





Also, you might want to consider subclassing the different types of animal later, but I guess it is superflous in the absence of animal-specific traits.

What's the thing about using pigs as weapons? Was that really supposed to be?

I'm sure the more experienced programmers can improve this code quite a bit.
akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
Mikael Saltzman wrote:Alright, this is a bit messy, but anyway:





Also, you might want to consider subclassing the different types of animal later, but I guess it is superflous in the absence of animal-specific traits.

What's the thing about using pigs as weapons? Was that really supposed to be?

I'm sure the more experienced programmers can improve this code quite a bit.


OMG Thanks a lot. Now i realize how much i messed up my own code. Could see the beauty of object oriented programming. But i am trying to print the weapons of each animal using formatter.format(). Still not getting it.. Do you have an idea about that.. ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
You can always print an object with its toString() method
System.out.format("%s%n", obj);
Where obj is a reference to any object. Of course you get peculiar results if you don’t override the toString() method.
Mikael Saltzman
Greenhorn

Joined: Dec 07, 2012
Posts: 9
Interesting, I've always used the "printf" instead of "format". It gives the same result in this case. Any pros of using one or the other?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Sorry for delay; I have been very busy with other things.

You did actually say format method earlier. If you look up System.out and its printf() and format() methods, you will see how much difference there is between them. Or more precisely how little difference. I think both create a Formatter behind the scenes and use its format() method, but I am not certain.
 
GeeCON Prague 2014
 
subject: storing and displaying 2d arrays