简体   繁体   English

你如何传递数组维度?

[英]How do you pass Array dimensions?

How do you pass Array dimensions along with the Array in java? 如何在java中传递数组维度和数组? I invision something like this: 我这样的东西:

public String[1][1] abc(){
return theStringArray
}    
//but this isn't possible

So is there a way to pass dimensions of arrays? 那么有没有办法传递数组的维度?

Right now I have 2 methods that pass an int for each dimension but is there a better way of doing this? 现在我有2个方法为每个维度传递一个int但是有更好的方法吗?

The problem at it's heart is this: 它的核心问题是:

When I try to pass the array and find it's length, it gives me a load of errors. 当我尝试传递数组并找到它的长度时,它给了我一大堆错误。 The problem is that the array for the class receiving the array needs to initialize the array the the passed array will copy to, but without the passed arrays dimensions, how can I initialize it? 问题是接收数组的类的数组需要初始化传递的数组将复制到的数组,但是没有传递的数组维度,我该如何初始化它?

Class the array is passed to: 数组传递给的类:

String[][] lordStats;
ArrayList<String> troopList;

public void loader() {
    lord lorder = new lord();
    lordStats = lorder.returnLord();
    total = lorder.returnLordTotal();
for (int i = 0; i < lordStats[0].length; i++)
    troopList.add (lordStats[i][2]);

}

Class the array comes from: note that method lord creator is called multipul times. 数组的类来自:注意方法主创建者被称为多次。

public class lord {
static int total;
String[][] lordStats;

public void total(int total1) {
    total = total1;
    System.out.println("lordTotal");

}

public void lordCreator(String lord, String kingdom, String troop, int times) {

    lordStats = new String[total][3];
    System.out.println("animalStats");

    lordStats[times][0] = lord;
    lordStats[times][1] = kingdom;
    lordStats[times][2] = troop;


}

public String[][] returnLord() {
    return lordStats;
}

In Java, you usually don't have to pass array dimensions along with the array, since every array implicitly knows its own dimensions. 在Java中,您通常不必将数组维度与数组一起传递,因为每个数组都隐式地知道它自己的维度。

If the array is called arr , then arr.length would return its size. 如果数组被称为arr ,那么arr.length将返回其大小。 If arr is a 2D array, then arr[0].length would give the size of the first row; 如果arr是2D数组,则arr[0].length将给出第一行的大小; arr[1].length is the size of the next row, and so on. arr[1].length是下一行的大小,依此类推。

If the function is to allocate an array of caller-specified size, simply pass the desired dimensions into the function as int arguments. 如果函数是分配调用者指定大小的数组,只需将所需的维度作为int参数传递给函数。

You don't have to explicitly pass array dimensions in Java, arrays have length properties for that. 您不必在Java中显式传递数组维度,数组具有长度属性。

public String[][] abc() {
    return new String[10][10];
}

You can figure out the length of an array with just .length . 你可以用.length数组的长度。 But Java arrays are just arrays of arrays (not N-dimensional blocks of contiguous elements). 但Java数组只是数组的数组(而不是连续元素的N维块)。

Thus, they can be jagged, meaning each row has a different number of columns. 因此,它们可以是锯齿状的,意味着每行具有不同数量的列。 So you can only rely on the length of theStringArray[0] (the column count for the first row) equaling theStringArray[1] if there is a documented understanding that theStringArray is not jagged. 所以你只能依赖于theStringArray[0]的长度(第一行的列数)等于theStringArray[1]如果有文件证明theStringArray没有锯齿状。

All arrays in Java are 1-dimensional. Java中的所有数组都是一维的。 You can have an array of arrays, which is effectively a 2-dimensional array. 您可以拥有一个数组数组,这实际上是一个二维数组。 However, each element can have a different length and there is no way to enforce a single second dimension size. 但是,每个元素可以具有不同的长度,并且无法强制执行单个第二维度大小。 You can do something like this: 你可以这样做:

public String[][] makeArray(int rows, int columns) {
    return new String[rows][columns];
}

Then you can query the array as follows: 然后您可以按如下方式查询数组:

String[][] array = makeArray(1, 1);
System.out.println("Row count: " + array.length);
System.out.println("Column count: " + array[0].length); // assumes >0 elements

However, someone could do this: 但是,有人可以这样做:

String[][] array = makeArray(3, 3);
array[0] = new String[1];

The previous code would then give a misleading result for the number of columns. 之前的代码会给出列数的误导性结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM