用Java处理多维数组



将多维数组作为对象形式的方法参数,然后在该方法中将其重建为变量的最佳方法是什么?我想将数组作为对象传入的原因是,我希望我的代码能够使用任何n维数组。我可以通过使用方法重载来避免这种情况,但制作数百个相同的方法来解释所有可能的数组维度似乎是一种非常糟糕的方法。然而,使用对象作为参数会带来一系列新的挑战,因为我无法初始化该数组,因为通常需要显式声明数组维度。基于我的一些研究,我找到了一种方法来确定作为对象传入的数组的维度,您可以在下面的代码片段中查看该数组。

public static void callTestArray() {
var matrix = new int[][]{{1,2}, {4, 6, 7}};
test(matrix);
}
public static void test(Object obj) {
final int dimensions = dimensionOf(obj);
System.out.println("Dimensions:" + dimensions);

//I can't create a variable from this though since I need to hard code the dimensions of the array
}
/**
* This returns the amount of dimensions an array has.
*/
public static int dimensionOf(Object arr) {
int dimensionCount = 0;
Class<?> c = arr.getClass(); // getting the runtime class of an object
while (c.isArray()) { // check whether the object is an array
c = c.getComponentType(); // returns the class denoting the component type of the array
dimensionCount++;
}
return dimensionCount;
}

我已经四处寻找了一段时间,但我找不到一个允许我在中传递任何n维数组的对象,该对象允许我轻松访问所有数组的典型信息?这是Java中没有包含的,还是我错过了它?话虽如此,由于255是数组的最大维数,我可以创建自己的utils类来处理这一问题,但处理所有情况都需要大量的冗余和精力。我只是想在浪费几个小时制作这样的东西之前,确保它还没有制作好。此外,如果有人对任何内部java库有更好的方法,请告诉我!

我们通常不使用像ArrayList这样的集合来传递数组,这允许我们进行一些抽象,并允许我们向其中添加一些常见的方法。请注意,ArrayList并没有扩展数组,它只是实现了一个列表接口。

我向您推荐同样的方法,不要传递数组,而是考虑将数组封装在一个类中并传递该类。使用该类可以进行某些简化,例如,您可能有一个方法允许它将一个函数应用于矩阵的每个元素,或者一个函数用于调整矩阵的大小。

你可以在不同的变量中跟踪矩阵的维度,这样你就可以在不重新分配数组的情况下调整矩阵的大小(就像ArrayList一样(

封装的另一个优点是,如果你想做一些不同的事情,比如用它制作一个稀疏矩阵,你可以在不改变使用方式的情况下重新实现底层代码(比如ArrayList和LinkedList有相同的接口,但对不同的用例做不同的事情(

你的其他条件似乎也适用于这个矩阵对象和数组,例如,你会将维度传递到构造函数中,以在最初创建它(尽管,正如我所说,你可以在稍后轻松地扩展它,特别是如果你在底层实现中使用了ArrayList的ArrayList,如果你需要的话(

我认为它没有包含在Java中的原因是它不是很常用,也不容易实现,但如果你真的不想自己做,apache有一个看起来很合适的Matrix实现。

我们经常使用时间序列数据,如每小时的温度(通常一天的分辨率低至10秒(,因此我们构建了自己的类,该类基本上表示图上y轴为"0"的一条线;日期";,类似于链表,但每个值都有时间戳。这个结构对我们来说非常有用,我经常想知道为什么它不在Java中,但我想我只是回答了自己的问题,使用得不够。

这是varargs:的作业

public static void main(String[] args) {
var matrix = new int[][]{{1,2}, {4, 6, 7}};
System.out.println("Length is: " + getSize(matrix));
}
public static int getSize(int[]... multiArray) {
return multiArray.length;
}

打印出来的:

Length is: 2

此外,除非您必须使用数组来保存int数组,否则我将使用ArrayList<int[]>相反这样你就可以很容易地添加到你的列表中,比如:

ArrayList<int[]> multiArray = new ArrayList<>();
multiArray.add(new int[]{1,2,3});
multiArray.add(new int[]{4,5,6});

然后你只需调用就可以得到它的大小

multiArray.size()

这是我的尝试。使用Object作为参数,然后在方法体中检查数组维度。在这个例子中,我只将其限制为三维阵列,但您可以向上扩展到任何维度。

public class Main{
static void process(Object o){
if (o instanceof int[]){
int[] a = (int[]) o;
System.out.println("1D. length is " + a.length);
} else if (o instanceof int[][]){
int[][] a = (int[][]) o;
System.out.println("2D. row=" + a.length + ", col=" + a[0].length);
} else if (o instanceof int[][][]){
int[][][] a = (int[][][]) o;
System.out.println("3D. row=" + a.length + ", col=" + a[0].length + ", depth=" + a[0][0].length);         
} else {
System.out.println("Unsupported array dimension.");
}
}

public static void main(String[] args) {
int[] a = {1,2,3};
int[][] b = {{1,2,3},{1,2,3}};
int[][][] c = {
{ {1,2,3}, {1,2,3} },
{ {1,2,3}, {1,2,3} }
};
process(a);
process(b);
process(c);
}
}

输出:

1D. length is 3
2D. row=2, col=3
3D. row=2, col=2, depth=3

最新更新