wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes List of String Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List of String Arrays" Watch "List of String Arrays" New topic
Author

List of String Arrays

Brian Winkler
Greenhorn

Joined: Jun 05, 2012
Posts: 16
Hello all...
Please look at the following snippet of code.



In the first debug section everything works as expected, and the output is consecutive strings of the results of the query.
In the second debug statement, the count is correct, but no matter which index I choose, the output is always the last entry that was added. If I put this in a looped output, I will get the total counts worth of the same thing line after line. It's always whatever the value of the last entry was. Can anyone please tell me what I am missing? I have been beating my head on a wall for a few days now, and I just can't see why it's not adding the lines consecutively.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I'm not sure that I understand that, so correct me if I'm wrong... Do you mean to say that in line 46, debug[0].toString() and debug[1].toString() and debug[2].toString() all return the same string?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Paul Clapham wrote:I'm not sure that I understand that, so correct me if I'm wrong... Do you mean to say that in line 46, debug[0].toString() and debug[1].toString() and debug[2].toString() all return the same string?

I think what he is saying is that no matter what index he uses on line 45, the only output would be for the input set in the last run of the loop. And this makes sense. You use a single array over and over again, in the loop you are simply changing the values in the single array then putting that array into the list again and again. Since you only have one array, all the indexes in the list point to the same array instance and the data is the same (whatever happens to be put in last).

If you want to store different values you need to use a different array for each iteration through the loop.


Steve
Brian Winkler
Greenhorn

Joined: Jun 05, 2012
Posts: 16
Paul Clapham wrote:I'm not sure that I understand that, so correct me if I'm wrong... Do you mean to say that in line 46, debug[0].toString() and debug[1].toString() and debug[2].toString() all return the same string?


so, I collect information from an API that gives me three items per player. I collect those in a String Array called nameOreStoneString in locations 0, 1, 2. (lines 32 - 34)
I then take that string array and add it to a list of arrays called namesAndOresList. (line 40)
I then output a count of the total lines added from line 40. (line 43)
Line 45 and 46 are the problem... I want to be able to output a row from the list, except when I do no matter what line I choose, I always get the same output.

Sample output:


My output here should be:


Hope this help to clarify my issue. Thanks for such a quick response.
Brian Winkler
Greenhorn

Joined: Jun 05, 2012
Posts: 16
Steve Luke wrote:
Paul Clapham wrote:I'm not sure that I understand that, so correct me if I'm wrong... Do you mean to say that in line 46, debug[0].toString() and debug[1].toString() and debug[2].toString() all return the same string?

I think what he is saying is that no matter what index he uses on line 45, the only output would be for the input set in the last run of the loop. And this makes sense. You use a single array over and over again, in the loop you are simply changing the values in the single array then putting that array into the list again and again. Since you only have one array, all the indexes in the list point to the same array instance and the data is the same (whatever happens to be put in last).

If you want to store different values you need to use a different array for each iteration through the loop.


Yes this...
So, how do I do that? I thought once I store the info into the List, I can reuse the String Array to create the next item. Is there a better way to do this instead of String array inside of list array?

Thank you for all your help I have a headache at this point from over thinking this.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

Brian Winkler wrote:
Yes this...
So, how do I do that? I thought once I store the info into the List, I can reuse the String Array to create the next item. Is there a better way to do this instead of String array inside of list array?

Thank you for all your help I have a headache at this point from over thinking this.


You are definitely over-thinking this.... basically, you have a loop that puts the same array object in the collection many many times. So, shouldn't you expect a collection with many many references to the same array?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brian Winkler
Greenhorn

Joined: Jun 05, 2012
Posts: 16
Henry Wong wrote:
Brian Winkler wrote:
Yes this...
So, how do I do that? I thought once I store the info into the List, I can reuse the String Array to create the next item. Is there a better way to do this instead of String array inside of list array?

Thank you for all your help I have a headache at this point from over thinking this.


You are definitely over-thinking this.... basically, you have a loop that puts the same array object in the collection many many times. So, shouldn't you expect a collection with many many references to the same array?

Henry


Yeah, I think I understand that part, so adding the String Array to the List Array is just a reference. So, I guess the question is how do I tell my program that each time it runs through the loop to use a new instance of the String Array? Or is there a way to add the actual data into the List Array instead of a reference? Or is all of this a really poor way of doing this, and I should be doing this a completely different way?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

We aren't being thick here, we just want you to work it out yourself.

So, I guess the question is how do I tell my program that each time it runs through the loop to use a new instance of the String Array?

How do you make an instance of an array? Note, you already do it once in your code. How do you think you could do the same thing: but once for each iteration of the loop?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

And with respect to:
Or is all of this a really poor way of doing this, and I should be doing this a completely different way?

Whereas I don't think this is a really poor way of doing it, I think a better way would be to create an Object which has field which represent the 3 pieces of information you need to keep together.
Brian Winkler
Greenhorn

Joined: Jun 05, 2012
Posts: 16
Steve Luke wrote:We aren't being thick here, we just want you to work it out yourself.

So, I guess the question is how do I tell my program that each time it runs through the loop to use a new instance of the String Array?

How do you make an instance of an array? Note, you already do it once in your code. How do you think you could do the same thing: but once for each iteration of the loop?


Sorry, I knew you were trying to get me to help myself, but my head really hurts at this point (not kidding, I have a terrible headache). I did figure out that if I moved the line 18 into the loop it solved the issue. So thank you so much for all the direction.
One more question... And I am pretty sure you can't make me learn this on my own but is this an okay way to be doing this? or is there a better type of collection or some other method I should look into? I figure this can only be answered by experience.

Thanks Again. This site never lets me down.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Brian Winkler wrote:One more question... And I am pretty sure you can't make me learn this on my own but is this an okay way to be doing this? or is there a better type of collection or some other method I should look into? I figure this can only be answered by experience.

Thanks Again. This site never lets me down.


Glad we can help. Like I said in my last post, I think the best approach is to start creating a class which encapsulates the data you want to keep together. In your code you clearly have 3 values you want to associate together as one 'thing:'
1) A player name
2) Some value which you get like this oreLookup(bc.playerName, oreid, world, since)
3) Some value which you get like this stoneLookup(bc.playerName, world, since)

Instead of using a String array to hold those three related values, how about a class which holds them. If we were to define a class, maybe do something like this:

Class Name: "PlayerResources"
Fields:
String player name
int ore
int stone
Constructors:
PlayerResources(String name): Build a new PlayerResources for a player with the given name and no resources
PlayerResources(String name, int ore, int stone): Build a new PlayerResources for a player with the given name, amount of ore, and stone
Methods:
String toString(): Take the current fields and translate them into a String which makes sense to print out
boolean equals(Object other): Check if the other Object represents the same as this
void setOre(int ore): Set the amount of ore
void setStone(int stone): Set the amount of stone
String getPlayerName(): Get the name of the player whose resources these are.
int getOre(): Get the current amount of ore this PlayerResource holds
int getStone(): Get the current amount of stone this PlayerResource holds

Now, this is a bit of work, and it might be overkill if you never expect to use these things again after creating them. But if you put the time into creating this class then you can reap some hefty benefits later on. This lets you be more descriptive about what you are getting (instead of nameOreStoneString[0] where you pretty much have to know that the 0th index is the name, you could do myPlayerResources.getPlayerName() and know exactly what you are getting (and more importantly, someone else who reads the code would not, and you will know 6 months from now). It also lets you treat the data as their natural types: the name as a String, the ore and stone as integers, rather than forcing them all into a single data type like String.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List of String Arrays