user1319364
user1319364

Reputation: 1

Adding ints to final array list

I have an array list (below). The numbers of the list's equivalent char representation print out a secret message (which can be done through type casting). But before I read that, I need to add 5 to each element in the array list first. But I thought as the array is final that we could not change the string elements? (I did try making the array non-final, but still could not increment each value in the list by 5.) You can see the code I have tried to use below, but it still prints out the original values in the list. Does anyone have any pointers? thanks.

public static void main(String[] args) {
    final int[] message = { 82, 96, 103, 103, 27, 95, 106, 105, 96, 28 };
    final int key = 5;
    for (int x : message)
        x = x + key;
    for (int x : message)
        System.out.print(x + ",");
}

Upvotes: 0

Views: 78

Answers (5)

TheLostMind
TheLostMind

Reputation: 36304

But I thought as the array is final that we could not change the string elements? I did try making the array non-final

You are getting confused between final and immutable :

final--> 1. For primitives : you can't change the value (RHS)
         2. For non-primitives : you can't reassign the reference to another object.
         2.b. You can change the value(s) of the object to which the reference is currently pointing.

immutable - you can't change the value of the object to which the reference is pointing.

Upvotes: 0

Yahya Arshad
Yahya Arshad

Reputation: 1626

You are adding key in copy of element of array, which will not change value of actual element of array. instead do this.

   for (int x =0; x < message.length; x++)
        message[x] = message[x] + key;
    for (int x : message)
        System.out.print(x + ",");

More clarification

int value = message[0];
value = value+10; // This will not change value of element at message[0];

Upvotes: 0

Drogba
Drogba

Reputation: 4346

Try this

final int[] message = { 82, 96, 103, 103, 27, 95, 106, 105, 96, 28 };
final int key = 5;
for (int i = 0; i < message.length; i++)
  message[i] += key;
for (int i = 0; i < message.length; i++)
  System.out.print(message[i] + ",");

Your code doesn't work because your x is a local variable in the for loop.

Upvotes: 0

stinepike
stinepike

Reputation: 54682

you are not changing the message array. you are just getting a temp value x for each element then increasing it. even if you tried it would show error as it is declared as final .

to increase the values you can do something like this

int[] message =
    {82, 96, 103, 103, 27, 95, 106, 105, 96, 28};
final int key = 5;
for (int i = 0; i< message.length; i++)
    message[i]+=key; 

Upvotes: 3

evanchooly
evanchooly

Reputation: 6233

you don't need that second loop:

for (int x: message) { x = x + key; System.out.print(x + ","); }

In the first loop, you're change a variable (x) local to that loop. You're not actually modifiying the array contents like you seem to be expecting. In the second loop, that x variable is local to the second loop and completely distinct from the x of the first loop.

Upvotes: 0

Related Questions