Yes, I know Java's Strings are immutable, but still, that alone does not explain why this won't work for me...
I ran into this problem when I tried to convert an array of strings (String[]
) to one string of the form: ('String[0]','String[1]',...,'String[n]')
.
For example, given the following array:
String[] array=new String[3];
array[0]="Jake";
array[1]="Mishel";
array[2]="Dan";
I wanted to create the string: ('Jake','Mishel','Dan')
But, the way to do it is not the purpose of this post...
What I'm interested to know is, why does this work:
String[] array=new String[3];
array[0]="Jake";
array[1]="Mishel";
array[2]="Dan";
for (int i=0; i<array.length; i++) {
array[i]=array[i]+"'";
array[i]="'"+array[i];
}
String str=Arrays.toString(array);
str=str.replace('[','(');
str=str.replace(']',')');
System.out.println(str); //produces ('Jake','Mishel','Dan'), as desired
But this doesn't:
String[] array=new String[3];
array[0]="Jake";
array[1]="Mishel";
array[2]="Dan";
for (String str: array) {
str=str+"'";
str="'"+str;
}
String str=Arrays.toString(array);
str=str.replace('[','(');
str=str.replace(']',')');
System.out.println(str); //produces wrong output: (Jake,Mishel,Dan)
?
-
what is the error you get?Kick Buttowski– Kick Buttowski2014年08月13日 18:53:21 +00:00Commented Aug 13, 2014 at 18:53
-
There's no error. just wrong output.so.very.tired– so.very.tired2014年08月13日 18:54:42 +00:00Commented Aug 13, 2014 at 18:54
-
Your loop creates local copies of the array into a new String object while, in the first exemple, the reference of element in the array is passed, its value is directly modifiedJako– Jako2014年08月13日 18:56:54 +00:00Commented Aug 13, 2014 at 18:56
-
3In general, "<Major programming language>'s <major feature> doesn't work" is false - you can almost always assume that you've just made an error. "Why can't I modify a String element of an array in a for each loop?" might be a better title.Michelle– Michelle2014年08月13日 19:00:56 +00:00Commented Aug 13, 2014 at 19:00
6 Answers 6
It doesn't work because in the second one, you're extracting the value of the string from each index in the array as str
, and then making changes to str
, not the string in the array (Technically not changes, replacing would be a better word). In the first one, you rewrite the index of the array with a new value.
In effect, in a for each loop you're working with a local variable with the same value as the global variable. However, this is a new variable, and so it doesn't have a scope outside of your loop. In the regular for loop, you explicitly update the item located at each index of the array.
4 Comments
str
each time around the loop. str
is not a String: str
is a local variable that refers to a String. And that's the problem! The reference initially stored in str
is a copy of the reference stored in the array. After str
is updated to refer to a new String, the array still refers to the old String. The first example updated the references in the array.This code
for (String str: array) {
str = str+"'";
str = "'"+str;
}
Is equivalent of
for (int i=0; i<array.length; i++) {
//Temporary local variable named str which holds value from array
String str = array[i];
str = str+"'";
str = "'"+str;
}
So by str=str+"'";
you are reassigning local variable which doesn't affect array, while by array[i]=array[i]+"'";
you actually update value in array.
BTW: Java 8 adds StringJoiner
class which can be used like
StringJoiner sj = new StringJoiner("','", "'", "'");//delimite, prefix, suffix
for (String str : array)
sj.add(str);
System.out.println(sj);
to produce 'Jake','Mishel','Dan'
We can also create one-line solution by using join
method added to String
System.out.println("'"+String.join("','", array)+"'");
// ^^^ - delimiter
Comments
for (String str: array) { // read as for each string str in Array array. str contains same value as arr[i]
str=str+"'";
str="'"+str;
}
Is your problem. you are working on copies of the actual strings of the array. You are not working on the array.
Comments
Inside your loop str
is effectively a local variable; it's not a reference to the element of the array but rather a new String which is a copy of that element. So your changes to str
do not change elements of the array.
Comments
The reason this
for (String str: array) {
str=str+"'";
str="'"+str;
}
Doesn't work is because you are modifying the reference String str
(which is independent of the array being a local copy).
First, you could declare and initialize your array with
String[] array = { "Jake", "Mishel", "Dan" };
Then
for (int i = 0; i < array.length; i++) {
array[i] = array[i] + "'";
array[i] = "'" + array[i];
}
Should probably be
for (int i = 0; i < array.length; i++) {
array[i] = "'" + array[i] + "'";
}
Finally, you could write
str=str.replace('[','(');
str=str.replace(']',')');
as
str = str.replace('[', '(').replace(']', ')');
Putting it together
String[] array = { "Jake", "Mishel", "Dan" };
for (int i = 0; i < array.length; i++) {
array[i] = "'" + array[i] + "'";
}
String str = Arrays.toString(array);
str = str.replace('[', '(').replace(']', ')');
System.out.println(str);
Output is
('Jake', 'Mishel', 'Dan')
Comments
It does not work because str
is not the String
in the array, it is a local variable that has the same characteristics as the "real" String
in the array / a copy of the String
in the array.