Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
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!
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
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).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have used java ArrayList when I inserted one element in the list and i have converted the validatable response to array list when I used assert equals it is showing that both are different
like one is [abcd] other is [[abcd]]
Validatable response = given().spec(request).filter(new ErrorLoggingFilter(errorPrintStream)).pathParams("","").when.post(endpoint).then()
the response is of the form ArrayList when I printed that It came of the form [[abcd]]
To my knowledge, these two are different things
["abcd"] this means an array has one string "abcd" element.
[["abcd"]] this means an array has one array ["abcd"] element.
Yes, ["abcd"] and [["abcd"]] are completely different. Let us understand why.
Let us consider an array ["abcd"]. As you can see, it contains only one element i.e. "abcd". So this is an array that contains a single string value. Now for [["abcd"]], the outer array contains another array inside of it and the inner array contains "abcd". Though their ultimate content seem to be same, they are absolutely different. One is a string array (an array that contains a string value) and the other is an array of string arrays.
Absolutely different, a one-dimensional array, a two-dimensional array,in many languages,reference form it, [0] and [0][0]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
For example, Let's say I have the code below:
ArrayList<String> A1 = new ArrayList<>();
A1.add("Hello");
A1.add("World");
A1.add("!");
Then I perform the following actions:
A1.remove(2);
So A1 is now ["Hello", "World"].
But how do the compiler perform this action? What will the pointer that points to "!" before point to after I perform the delete action? Apparently A1.get(2) is now an illegal operation and will cause IndexOutOfBounds Exception, and A1.size = 2. What happens to the space that is allocated for 3rd slot of the list in memory and how are all these processed at lower level?
Thank you!
ArrayList internally creates array and stores data in that. When you remove element from ArrayList:
If it is an last element, it will just place null in that last index of array.
If it is an non last element, it will shift all the element from index+1 to left 1 place. and in the last index it will place null.
So once the particular index of array is placed with null that the object which is placed there becomes de-referenced(If the same object is not referred by any other reference), and it will be available for garbage collector.
In your case at the 2nd index null will be placed and "!" becomes un-referenced. So it will be available for garbage collector.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to finish an assignment in my intro to Java course, and I have some questions. First off, what does it mean when there is a -- in FRONT of an int value? Also what is a String Builder? I had some help but want to understand what it is I'm using in the code. Thanks.
The -- in front of a value simply means subtract 1 from it. Similarly, ++ in front of a value means add 1 to it.
If you write ++
before the number it is called prefix operator and if after then its post fix
preFix: ++a will increase the value before using it, will first increase and then use it.
postFix a++ will first use it and then use it, for later use you will get the incremented value.
-- is a predecrement
Java StringBuilder class is used to create mutable (modifiable) string.
A String is immutable i.e string cannot be changed once created and everytime a value is change it create new string.
But in case of StringBuilder which is mutable string can change.
My experience is mostly with C# not Java, but in C# strings cannot be changed, when you concatenate two strings like "hello" + "world" you do not change either string, you create a new one and the old two still exist. If you need to do this many times (dozens or hundreds) it can use a lot of memory. A StringBuilder allows you to conserve memory by appending characters to the same block of memory while you are building your string, and then you can turn the result into a normal string for passing around to other functions.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Does anyone know how can I populate an array that checks if the positions are filled before filling them, and if they are already filled and if that array position is filled it increments to the next one.
Thanks
Primitive array elements are never empty. For example in an int array all elements will be initialized to 0.
So if you want to check for filled or not, initialize the array elements to a value which it is not going to take. say -1. Then each time you make an entry check for -1. The dummy value initialization is necessary as you can not check for 0, because 0 can be a valid data as well
// Initialization part
int [] arr= new int [17];
for(int i=0;i<arr.length;i++)
{
arr[i]= -1;
}
Hope you will do the checking part yourself
I am assuming (possibly incorrectly) that you either a) want a method that inserts object into the first available (i.e. not null) spot in the array at or past the argument integer or b) want to insert repeatedly into the array using the method in a)
the method in a) would basically be as follows
public <Type> void myInsertMethod(Type[] array,Type item,int position)
{
while (array[pos]!=null)
{pos++;}
array[pos]=item;
}
if you want to do this repeatedly on the same array, just call the method repeatedly, with different positions and items.
You can check if the position you are going to fill it's not null, or empty, or at least the stored value it's different to the one you are going to put
if(array.get(index) != "" && array.get(index) != null) && array.get(index) != newObject){
array.add(newObject);
}
Then, depending on the stored value type, you can ddo some more checks, for example in the cas e of the int, as RookieB says, you can check if the object is different to 0
Hope it helps
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
ive hit a road block and im stuck with a question on my work.
Here is the question:
Provide an implementation of the getLoad method that adds up the individual weights of
the items in the items list and returns the total.
The items list is:
ArrayList<Item> items;
Ive done what i thought was right but for some reason its not working.
Any help on what is wrong, or if what im doing is wrong? thanks
#Override
public int getLoad() {
int load = 0; //declare the variable
for (Item i : items) { // for each item in the list of items
load = load + i.getWeight() ; // load equals the weight of the item and adds on
}
return load; //returns it
}
The one thing I could see going wrong is if getWeight() returns a double. In that case, you should make int load: double load instead
With the limited information you've provided here, I can only take a wild guess that the problem is either:
The list items is empty even though it shouldn't be. Make sure that Items actually get added to the list! Use a debugger or a printed message to find out if the item list is empty on getLoad().
Weights for each Item are not assigned correctly, so getWeight() returns zero for each of them. Make sure that each Item added to the list actually gets assigned its proper weight.
Posting more code would help us give a better answer.