如何在客户端类

时间:2017-02-27 21:49:55

标签: java arrays multidimensional-array

我正在构建下面的客户端类,我只是想知道Airplane是否等于或者是(2-D数组,2-D数组),我怎么能在第二个数组中进入statment Airplane steveo =下面的新飞机(2-D阵列,2-D阵列)。

public class AirplaneClient
{
    public static void main(String [] args)
    {
        Airplane steveo = new Airplane({"o ", "o ", "o ","o "}
                                        {"o ", "o ", "o ","o "}
                                        {"o ", "o ", "o ","o "}
                                        {"o ", "o ", "o ","o "}
                                        {"o ", "o ", "o ","o "}
                                                               );
    }
}

如果你仍然对我的问题提出的问题感到困惑,我基本上是在尝试执行以下代码:

public class AirplaneClient
{
    public static void main(String [] args)
    {
        Airplane steveo = new Airplane(2-D Array, 2-D Array);
    }
}

我知道这可能是什么语法:

public class AirplaneClient
{
    public static void main(String [] args)
    {
        Airplane steveo = new Airplane({"o ", "o ", "o ", "o "}
                                       {"o ", "o ", "o ", "o "}
                                       {"o ", "o ", "o ", "o "}
                                       {"o ", "o ", "o ", "o "}
                                       {"o ", "o ", "o ", "o "},

                                       {"o ", "o ", "o ", "o "}
                                       {"o ", "o ", "o ", "o "}
                                       {"o ", "o ", "o ", "o "}
                                       {"o ", "o ", "o ", "o "}
                                       {"o ", "o ", "o ", "o "});
    }
}

但是,我对此并不乐观。请帮忙!

PS:

如果这也有帮助,这里是Airplane的构造函数:

import java.util.*;
public class Airplane
{
    private String[][] first;
    private String[][] economy;
    public Airplane()
    {
        first = new String[5][4];
        economy = new String[30][6];
        for (int i = 0; i < 5; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                first[i][j] = "o ";
            }
        }
        for (int i = 0; i < 30; i++)
        {
            for(int j = 0; j < 6; j++)
            {
                economy[i][j] = "o ";
            }
        }
    }

    public Airplane(String[][] first1, String[][] economy1)
    {
        first = first1;
        economy = economy1;
    }

3 个答案:

答案 0 :(得分:1)

我认为更好的解决方案是制作一个新的数组,如果该类型具有维度[a] [b] [c] [d],填充它,然后将其作为参数传递。

答案 1 :(得分:0)

首先将构造函数输入更改为:

public Airplane(String[][] first1, String[][] economy1) {
        first = first1;
        economy = economy1;
}

然后称之为:

Airplane steveo = new Airplane(
                new String[][] { 
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "} 
                }
                , 
                new String[][] { 
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "} 
                });

答案 2 :(得分:0)

private String[][] first;
public void setFirst(String[][] first){this.first = first;}
private String[][] economy;
public void setEconomy(String[][] eco){this.economy=eco};

//outside class
String[][] one = new String[][] {
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "}
            };
String[][] two = new String[][] {
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "},
                    {"o ", "o ", "o ", "o "}
            };
setFirst(one);
setEconomy(two);

然后