Set Individual Elements
You can also set any of the elements of an array to have a new value.
To do this, on the left hand side of an equals sign you write the name of a variable
followed by [
, an index, and ]
. Then on the right hand side of the equals you write
the new value.1
void main() {
String[] sentence = { "you", "are", "found", "guilty" };
System.out.println(
sentence[0]
+ " "
+ sentence[1]
+ " "
+ sentence[2]
+ " "
+ sentence[3]
);
sentence[1] = "aren't";
System.out.println(
sentence[0]
+ " "
+ sentence[1]
+ " "
+ sentence[2]
+ " "
+ sentence[3]
);
}
The index of the element to set can also come from a variable.
void main() {
int index = 2;
String[] response = { "and", "it", "isn't", "opposite", "day" };
System.out.println(
response[0]
+ " "
+ response[1]
+ " "
+ response[2]
+ " "
+ response[3]
+ " "
+ response[4]
);
response[2] = "is";
System.out.println(
response[0]
+ " "
+ response[1]
+ " "
+ response[2]
+ " "
+ response[3]
+ " "
+ response[4]
);
}
If you give a number equal to or greater than the length of the array or a number less than zero, you will get an error.
void main() {
String[] response = { "objection" };
// Crash
response[1] = "!";
}
void main() {
String[] response = { "sustained" };
// Crash
response[-1] = "not";
}
1
You cannot change the contents of a String
like you would an array. This is one of the biggest differences between a String
and a char[]
.