I assume that in its main it has something like this:
MyDesk barco = new MyDesk();
EnemyDesk enemigo = new EnemyDesk();
If the above compiles, you have a class of type EnemyDesk, but in this part public MyDesk (int [][] EnemyDesk){
use the same word I do not know if what I wanted is to be able to pass an EnemyDesk instance or simply pass the convention, I think and I think it is the second one, because inside the constructor you treat EnemyDesk as an array of integrated types of some kind, among other things but I think that at first glance it is confused by the simple fact of seeing int [][] EnemyDesk
and previously putting EnemyDesk enemigo = new EnemyDesk();
.
Now if I understand your question well, maybe what I'm looking for is something like this:
public static void main (String[] args) throws java.lang.Exception {
MyDesk barco = new MyDesk();
//EnemyDesk enemigo = new EnemyDesk();// es confusa esta parte
int [][] enemyDesk = null;
MyDesk barco1 = new MyDesk(enemyDesk);
}
class MyDesk {
int [][] barcosvivos;
public MyDesk(){
this.barcosvivos = new int[5][5];
barcosvivos [1][0] = 1; barcosvivos [2][0] = 1; barcosvivos[3][0]=1;
barcosvivos [1][2]= 1; barcosvivos [1][3]= 1; barcosvivos [1][4]= 1;
for (int []interno: this.barcosvivos){System.out.println(Arrays.toString(interno));}
}
public MyDesk (int [][] enemyDesk){
enemyDesk = new int [5][5];
enemyDesk [0][2] = 3; enemyDesk [0][3] = 3; enemyDesk [0][4] = 3;
enemyDesk [2][0] = 3; enemyDesk [2][1] = 3; enemyDesk [2][2] = 3;
for (int []interno2: enemyDesk){System.out.println(Arrays.toString(interno2));}
}
}
Test ideone
PS: to understand what you say about the convention, you can look at this: link
I do not know if this is the behavior I expected when I design it, according to this you have to call the two constructors to show the two outputs, for example:
-
MyDesk barco = new MyDesk(); --> llama a -> public MyDesk(){
-
MyDesk barco1 = new MyDesk(enemyDesk); --> llama a -> public MyDesk
(int [][] enemyDesk){
If you want to call both independently of the constructor that is called initially you can use this
if you do not know how to work with them or how they work you can look at these answers in some way how to apply it:
Using Extends in Java
Update by comment:
Hello Angel, I have tried with your correction and I effectively achieve that
two boards appear, but both are the same,
I do not understand what he says with both are the same:
ship:
[0, 0, 0, 0, 0]
[1, 0, 1, 1, 1]
[1, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
barco1:
[0, 0, 3, 3, 3]
[0, 0, 0, 0, 0]
[3, 3, 3, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
that is, in both cases the MyDesk () dashboard is displayed instead of
that in the second one it appears with the variations of MyDesk (int [] []
enemyDesk)
If you want to see what happens to you, you have to make some changes:
Sample main:
int [][] EnemyDesk = null;
EnemyDesk = new int [5][5];
EnemyDesk [0][2] = 1;
EnemyDesk [0][3] = 1;
EnemyDesk [0][4] = 1;
EnemyDesk [2][0] = 3;
EnemyDesk [2][1] = 3;
EnemyDesk [2][2] = 3;
MyDesk barco1 = new MyDesk(EnemyDesk); //<- le pasas EnemyDesk
Class MyDesk, change this part:
public MyDesk (int [][] EnemyDesk){
for (int []interno2: EnemyDesk){System.out.println(Arrays.toString(interno2));}
}
Notes: I recommend that you analyze the answer of @ MarceloZárate because if it is functional for what you are looking for, it has a better design in my opinion than the one you are showing in your question (but I do not know if that is suitable for what search) if so, I could add something like the following to the aforementioned reply:
public MyDesk(int [][] EnemyDesk){
barcosvivos = new int[5][5];
barcosvivos [1][0] = 1; barcosvivos [2][0] = 1; barcosvivos[3][0]=1;
barcosvivos [1][2]= 1; barcosvivos [1][3]= 1; barcosvivos [1][4]= 1;
barcosenemigos = EnemyDesk;
}