Note that there are some explanatory texts on larger screens.

plurals
  1. POGet and set methods not changing the array
    primarykey
    data
    text
    <p>I have extended a class in hope to store a global array (make the array within the class be seen by another object) by using the set1sub(sub.local) method</p> <pre><code>public class BattleShipsClient extends ShipLocations implements Runnable, BattleShipConstants { BattleShipsClient() { } public void placeBoard(int player) throws IOException// this is only called once and not updated when clicked again { while(getLocations()) { while(isWaiting())//true { toServer.writeInt(player); int row = getRowSelected(); int col = getColumnSelected(); int choice=Integer.parseInt(JOptionPane.showInputDialog("Please 1 for a sub, 2 for a battleship and 3 for a destroyer")); clickCount ++; if(clickCount &gt;2) { setLocation(false); continueToPlay=true; } if (choice ==1 &amp;&amp;sub.placed==false) { String orientation =JOptionPane.showInputDialog("please enter your orientations"); if(orientation.equalsIgnoreCase("h")) { //row os the same //column number will be sent, then plus one on the other side sub.local = new int[2][2]; sub.local[0][0] =row; sub.local[0][1]=col; sub.local[1][0]=row; sub.local[1][1] =col+1; toServer.writeInt(row); toServer.writeInt(col); toServer.writeChar('s'); sub.placed=true; setp1sub(sub.local); /*setp1sub(new int[][]{{row,col}, {row,col+1}});*/ grid[row][col+1].setToken(getMyToken()); } </code></pre> <p>I then have a ship Locations class however when i create a new object of the ship locations class and try to read this array it always is set to [[0, 0], [0, 0]], ive tried making it static and atomic</p> <pre><code>public class ShipLocations { int [][] p1sub; public ShipLocations() { p1sub = new int[2][2]; } public int[][] getp1sub() { return p1sub; } public void setp1sub(int[][] local) { for (int i = 0;i &lt;local.length;i++) { for(int j = 0;j&lt;local.length;j++) { p1sub [i][j]= local[i][j]; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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