Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的多维数组_Java - Fatal编程技术网

Java中的多维数组

Java中的多维数组,java,Java,好吧,我职业生涯的大部分时间都在使用PHP,我发现自己需要使用Java。在我的例子中,我遇到的最大问题是用Java创建和操作数组。 PHP中的示例: $cars = array(1 => array("stats"=> array("velocity"=>100, "acceleration"=>0, "en

好吧,我职业生涯的大部分时间都在使用PHP,我发现自己需要使用Java。在我的例子中,我遇到的最大问题是用Java创建和操作数组。 PHP中的示例:

   $cars = array(1 => array("stats"=> array("velocity"=>100,
                                        "acceleration"=>0,
                                        "energy"=>30000,
                                        "distance"=>200
                                      )
                      ),
                 2 => array("stats"=> array("velocity"=>3,
                                        "acceleration"=>6,
                                        "energy"=>30000,
                                        "distance"=>200)
                                      )
                      );

我试图在Java中重新创建这种类型的数组,但在初始化时遇到了问题。在这种情况下,数组是否被视为字符串?在创建阵列之前,必须设置阵列的大小吗?e、 g:
String[][]]car=newstring[][][]

我认为Java没有TRUE多维数组。像
a[i][j][k]
这样访问的数组只是一个数组,一个数组,一个数组

您可以尝试以下构造:

 String[][] car = new String [][] { { "X0", "Y0"},
                                    { "X1", "Y1"},
                                    { "X2", "Y2"},
                                    { "X3", "Y3"},
                                    { "X4", "Y4"} };

我认为Java没有TRUE多维数组。像
a[i][j][k]
这样访问的数组只是一个数组,一个数组,一个数组

您可以尝试以下构造:

 String[][] car = new String [][] { { "X0", "Y0"},
                                    { "X1", "Y1"},
                                    { "X2", "Y2"},
                                    { "X3", "Y3"},
                                    { "X4", "Y4"} };
1) 那是“统计数据”索引吗?如果没有,您可以:

Map<String, Integer>[] cars = new HashMap<String, Integer>[your length here];
2) 如果
“stats”
索引是nesserly,那么您必须深入一个维度1)这是
“stats”
索引nesserly吗?如果没有,您可以:

Map<String, Integer>[] cars = new HashMap<String, Integer>[your length here];

2) 如果
“stats”
索引是nesserly,那么您就必须像aet在评论中所说的那样深入一个维度,如果您正在考虑用java进行此操作,请不要这样做。你做错了

你应该有一节汽车课

public class Car {
   private int velocity;
   private int acceleration;
   private int energy;
   private int distance;
   //getters and setters, a constructor that gets all the property values ...
}
然后把你的车收藏起来。ArrayList是最简单的方法:

List<Car> cars = new ArrayList<Car>();
cars.add(new Car(100,0,30000,200));
cars.add(new Car(3,6,30000,200));

正如aet在评论中所说的那样——如果你正在考虑用java做这件事——不要这样做。你做错了

你应该有一节汽车课

public class Car {
   private int velocity;
   private int acceleration;
   private int energy;
   private int distance;
   //getters and setters, a constructor that gets all the property values ...
}
然后把你的车收藏起来。ArrayList是最简单的方法:

List<Car> cars = new ArrayList<Car>();
cars.add(new Car(100,0,30000,200));
cars.add(new Car(3,6,30000,200));

PHP中的内容通常在Java中表示为嵌套的
Map
实例。例如:

HashMap<Integer,Map<String,Map<String,Integer>>> data = new HashMap<>();

像这样填充嵌套映射可能会很复杂,在添加数据成员之前,通常会使用helper方法确保所有“父”级别都已填充。

在PHP中的内容通常在Java中表示为嵌套的
Map
实例。例如:

HashMap<Integer,Map<String,Map<String,Integer>>> data = new HashMap<>();

像这样填充嵌套映射可能会很复杂,在添加数据成员之前,通常会使用帮助器方法确保填充所有“父”级别。

是的,初始化数组时必须提供长度。因此,您的数组将如下所示:

cars.get(0).getVelocity(); //collection indexes start at 0
int lenght1=x;
int length2=y;
int lenght3=z;
String[][][] car = new String[lenght1][lenght2][lenght3]
我本人不是PHP开发人员,但数组中的类在抽象和继承方面将遵守Java实现的OOP规则。因此,当您检索元素时,您可以使用它们相应的接口,无论包含它们的类或接口是什么

另一方面,如果在初始化之前无法知道数组长度,则可以使用类ArrayList,它几乎类似于向量。如果添加新元素,此类将修改其内部长度。除了ArrayList之外,Java规范中还有一套完整的数据结构来存储元素,如映射、集合、列表等

在实例化ArrayList时,应该指定哪个类或接口将描述存储在数据结构中的对象,因此必须使用泛型来实例化该结构。就你而言:

ArrayList<String> dim1=new ArrayList<String>();
ArrayList<ArrayList<String>> dim2=new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<ArrayList<String>>> dim3= new ArrayList<ArrayList<ArrayList<String>>>();
ArrayList dim1=新的ArrayList();
ArrayList dim2=新的ArrayList();
ArrayList dim3=新的ArrayList();

正如你所看到的,这个结构比上面简单的数组更性感,但显然需要更多的注意来处理它。在将ArrayList放入3d矩阵之前,请不要忘记实例化它们,否则稍后会因访问空对象而出现异常。

是的,初始化数组时必须提供长度。因此,您的数组将如下所示:

cars.get(0).getVelocity(); //collection indexes start at 0
int lenght1=x;
int length2=y;
int lenght3=z;
String[][][] car = new String[lenght1][lenght2][lenght3]
我本人不是PHP开发人员,但数组中的类在抽象和继承方面将遵守Java实现的OOP规则。因此,当您检索元素时,您可以使用它们相应的接口,无论包含它们的类或接口是什么

另一方面,如果在初始化之前无法知道数组长度,则可以使用类ArrayList,它几乎类似于向量。如果添加新元素,此类将修改其内部长度。除了ArrayList之外,Java规范中还有一套完整的数据结构来存储元素,如映射、集合、列表等

在实例化ArrayList时,应该指定哪个类或接口将描述存储在数据结构中的对象,因此必须使用泛型来实例化该结构。就你而言:

ArrayList<String> dim1=new ArrayList<String>();
ArrayList<ArrayList<String>> dim2=new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<ArrayList<String>>> dim3= new ArrayList<ArrayList<ArrayList<String>>>();
ArrayList dim1=新的ArrayList();
ArrayList dim2=新的ArrayList();
ArrayList dim3=新的ArrayList();
正如你所看到的,这个结构比上面简单的数组更性感,但显然需要更多的注意来处理它。在将ArrayList放入3d矩阵之前,不要忘记实例化它们,否则您将在以后遇到访问空对象的异常情况。

试试这个

    Map<Integer, HashMap<String, HashMap<String, Integer>>> map = new HashMap<>();
    HashMap<String, Integer> hm = new HashMap<>();
    hm.put("Velocity", 1);
    hm.put("acceleration", 2);
    HashMap<String, HashMap<String, Integer>> state1 = new HashMap<>();
    state1.put("state1", hm);
    map.put(1, state1);
    System.out.println(map);
Map Map=newhashmap();
HashMap hm=新的HashMap();
hm.put(“速度”,1);
hm.put(“加速度”,2);
HashMap state1=新的HashMap();
状态1.投入(“状态1”,hm);
地图放置(1,状态1);
系统输出打印项次(map);
试试这个

    Map<Integer, HashMap<String, HashMap<String, Integer>>> map = new HashMap<>();
    HashMap<String, Integer> hm = new HashMap<>();
    hm.put("Velocity", 1);
    hm.put("acceleration", 2);
    HashMap<String, HashMap<String, Integer>> state1 = new HashMap<>();
    state1.put("state1", hm);
    map.put(1, state1);
    System.out.println(map);
Map Map=newhashmap();
HashMap hm=新的HashMap();
hm.put(“速度”,1);
hm.put(“加速度”,2);
HashMap state1=新的HashMap();
状态1.投入(“状态1”,hm);
地图放置(1,状态1);
系统输出打印项次(map);

在java中,你可能会有一些类车来表示汽车,在内部它会包含加速度、能量、距离等内容。然后你会有一个汽车数组,Car[],或者更好的,List,或者ArrayList。在java中,你可能会有一些类车来表示汽车,在内部它会包含加速度等内容,能量、距离等,然后你