-3

I have this code

for (int i=0; i<tini.length; i++){
    tini[i].tempLabel.setText("Temp: "+ Float.toString(tempArray[i]) +"°" );
    out_status[i] = tini[i].alarm;
    frame.statusLabel.setText("Connetction: OK, String: OK");
}

System.out.println("old: " + Arrays.toString(out_status_old));                  
System.out.println("new: " + Arrays.toString(out_status));
if (Arrays.equals(out_status, out_status_old) ){
    System.out.println("UGUALI");
}

out_status_old = out_status;

the resulting arrays are always equal. I cannot understand the reason. Using a Button in JFrame, in a GUI interface i can modify the value of alarm, but both the old value and the actual one change at the same time!

tobias_k
  • 81,265
  • 12
  • 120
  • 179
T. L.
  • 141
  • 1
  • 4
  • 5
    For better help, consider creating and posting a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve). We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem. You might very well solve the problem yourself by simply trying to isolate and expose the bug. – Hovercraft Full Of Eels Feb 14 '16 at 16:34
  • 1
    `out_status_old = out_status;` does not copy the array. You have just two variables pointing to _the same_ array. – tobias_k Feb 14 '16 at 16:34
  • I removed one line of the code because it's completely irrelevant to the question and everything thinks it's the actual problem – Joe Phillips Feb 14 '16 at 16:47

2 Answers2

0

When you access and then update the elements of one array, you're also updating the elements of the other array because they are referencing the same objects. You need to create separate items within each array while populating the arrays.

You left out an important part of your program which is where you're actually populating these arrays. Odds are, you are not doing a deep copy.

Deep copy of an object array

Community
  • 1
  • 1
Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
  • The only place where OP is changing elements in an array is this:`tini[i].tempLabel.setText`. Do you think that _this_ is the problem? Then why is OP checking `Arrays.equals(out_status, out_status_old)` and wondering why those are "uguali"? – tobias_k Feb 14 '16 at 17:44
-1

The line out_status_old = out_status; does not create a copy of the array. You have just two variables, out_status and out_status_old, pointing to the same array.

If you want to create a proper copy of the array, you can e.g. use Arrays.copyOf (or one of its variants).

tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • That is not the problem, it's just the next step of the program. It's not relevant to the question – Joe Phillips Feb 14 '16 at 16:40
  • @JoePhilllips Why do you think the line is irrelevant? The code is probably executed in a loop, or it is a callback that is executed multiple times. So a line that's after the block of code may very well affect the next execution of that code. – tobias_k Feb 14 '16 at 17:32
  • because the check he's doing to compare is above that line – Joe Phillips Feb 14 '16 at 17:41
  • @JoePhilllips You are aware of the concept of loops, and that a line of code that is _below_ another line could be executed _before_ that line, right? – tobias_k Feb 14 '16 at 17:42