?
Solved

Java Variable Reference Problem (with Vector elements)

Posted on 2007-08-05
9
Medium Priority
?
360 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:StephenSpeaks
  • 3
  • 3
  • 3
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19633587
Why not just set the value in 'extract' directly?
0
 
LVL 8

Expert Comment

by:mnrz
ID: 19633600
if you want to add new changes as new element you should clone them
0
 

Author Comment

by:StephenSpeaks
ID: 19633611
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Assisted Solution

by:mnrz
mnrz earned 200 total points
ID: 19633615
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
 

Author Comment

by:StephenSpeaks
ID: 19633620
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 19633624
String newRow = new String[original.length];
System.arraycopy(original, 0, newRow, 0, newRow.length);
newRow[0] = "mouse";
extract.add(newRow);
0
 
LVL 8

Expert Comment

by:mnrz
ID: 19633636
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
 

Author Comment

by:StephenSpeaks
ID: 19633730
thanks guys! You both gave solutions that worked, but I used CEHJ's suggestions of using System.arraycopy()

thanks again!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19633767
:-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question