Java Variable Reference Problem (with Vector elements)

I have this code:

        for (int i=0;i<length;i++){
            String[] record = new String[((String[])extract.elementAt(j)).length];
            record = (String[])extract.elementAt(j);
            int fieldPos = findField(fieldName);
            record[fieldPos] = info.substring(i,i+1);
            extract.addElement(record);
        }

"extract" is a Vector element containing String arrays

What I want to do is take an existing String[] from "extract" and store it in "record". Then, I want to modify a certain array value in "record". Then, when the value has been modified, I will add "record" into "extract" as new element.

I'm able to do these things, however, my problem is that aside from me being able to change the value of "record", the value of the original Vector element I'm copying from also changes.

I think this has to do with variable references and stuff, but I can't think of how exactly I should be fixing it.
StephenSpeaksAsked:
Who is Participating?
 
CEHJCommented:
String newRow = new String[original.length];
System.arraycopy(original, 0, newRow, 0, newRow.length);
newRow[0] = "mouse";
extract.add(newRow);
0
 
CEHJCommented:
Why not just set the value in 'extract' directly?
0
 
mnrzCommented:
if you want to add new changes as new element you should clone them
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
StephenSpeaksAuthor Commented:
Lets say "extract" has one String[] element that has this value:

{"cat","dog","horse"}

Now, what I want to do is to copy that String[] element, and then modify it, and then insert it into "extract" with its modified value.

Say, I wanted to duplicate the record and change "cat" to "mouse"... what I want to happen is end up with a Vector of these elements:

{"cat","dog","horse"}
{"mouse","dog","horse"}

However, what I end up with right now is:

{"mouse","dog","horse"}
{"mouse","dog","horse"}

Basically, the problem is that the original String[] array where I'm copying from also gets modified...
0
 
mnrzCommented:
because elementAt() returns an Object so simplest and first way is to write a for loop for each String and create new String object for each one

instead of this line:
record = (String[])extract.elementAt(j);

a for loop

String[] strings = (String[])extract.elementAt(j);
int i = 0;
for(String s: strings){
   record[i] = new String(s);
   ++i;
}

---
it's better to use StringBuffer for better performance.

this is not best solution, maybe the others help you with a breakthrough idea
0
 
StephenSpeaksAuthor Commented:
mnrz, is this what you were suggesting?

        for (int i=0;i<length;i++){
            Vector clone = (Vector)extract.clone();
            String[] record = new String[((String[])clone.elementAt(j)).length];
            record = (String[])clone.elementAt(j);
            int fieldPos = findField(fieldName);
            record[fieldPos] = info.substring(i,i+1);
            extract.addElement(record);
        }

I did this, but I'm still getting the same problem...
0
 
mnrzCommented:
No
what about this:

for (int i=0;i<length;i++){
            String[] record = new String[((String[])extract.elementAt(j)).length];
            String[] temp;
            temp = (String[])extract.elementAt(j);
            record = temp.clone();
            int fieldPos = findField(fieldName);
            record[fieldPos] = info.substring(i,i+1);
            extract.addElement(record);
        }
0
 
StephenSpeaksAuthor Commented:
thanks guys! You both gave solutions that worked, but I used CEHJ's suggestions of using System.arraycopy()

thanks again!
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.