Note that there are some explanatory texts on larger screens.

plurals
  1. POWondering about classes in arrays?
    text
    copied!<p>I have a script with the following code:</p> <pre><code> import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class app { public static int p; public static int b=0; public static int i; public static int add; public static List&lt;Integer&gt; a = new ArrayList&lt;Integer&gt;(); public static List&lt;Node&gt; c = new ArrayList&lt;Node&gt;(); public static void main(String[] args){ Random rand = new Random(); for( i = 0;i&lt;=10;i++){ int random = getRandom(); if(random!=0){ a.add(random); }else if(random == 0){ } } Collections.sort(a); System.out.println(a); for(int z = 1;z&lt;a.size();z++){ Node n = new Node(); Node.pos=a.get(z); System.out.println("Node pos set to "+a.get(z)); c.add(n); System.out.println("Node at " + n.pos); } System.out.println("class array length " + c.size()); System.out.println("Node at index " + 0 +" has a pos of " + c.get(0).pos); } public static int getRandom(){ Random randd = new Random(); int randomnum = randd.nextInt(10); if(!a.contains(randomnum)){ return randomnum; }else if(a.contains(randomnum)){ } return 0; } } </code></pre> <p>Now what I'm trying to accomplish with this, is I want the Node <code>n</code> that's created to be added to an array, which holds all the nodes and their positions. But it seems like once I create another node, all the nodes before it change their pos to that one.</p> <p>For example, I can try <code>c.get(0).pos</code> and it will return the same number as the one at 9, or however big the array is. How do I make every node that's added to the array keep its values? What's the problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload