简体   繁体   English

如何用Java制作一个数组? (没有ArrayLists)

[英]How can I make an array of my arrays in Java? (WITHOUT ArrayLists)

I have a few multidimensional arrays of type char[][]. 我有一些char [] []类型的多维数组。 Eg.. 例如..

char[][] ARRAY_1 = {
    {'.','#'},
    {'$','@'}
}
char[][] ARRAY_2 = {
    {'.','#'},
    {'$','@'}
}

And I want to make an array or list of some sort such as 我想做一个数组或诸如此类的列表

ARRAY = {ARRAY_1,ARRAY_2,...}

so I'll be able to put in ARRAY[1] (or something similar) and have it return the entire char[][] ARRAY_1 这样我就可以放入ARRAY [1](或类似的东西)并让它返回整个char [] [] ARRAY_1

I am very new to programming with Java so I'm not sure what the best way to do this is. 我对使用Java编程非常陌生,因此不确定执行此操作的最佳方法是什么。

Edit: I've just found out I'm not allowed to use ArrayLists. 编辑:我刚刚发现我不允许使用ArrayLists。

Direct answer: use ArrayList<char[][]> or char[][][] . 直接答案:使用ArrayList<char[][]>char[][][]

Basically, you create an ArrayList that holds your 2 dimensional arrays or a 3 dimensional array of chars. 基本上,您创建一个ArrayList来保存2维数组或3维char数组。

List<char[][]> array = new ArrayList<>();

or 要么

char[][][] array = char[length][][];

To add the arrays, you just use the following: 要添加数组,只需使用以下命令:

array.add(arrayOne); //for an ArrayList
array.add(arrayTwo);

or 要么

array[0] = arrayOne; //for an array
array[1] = arrayTwo;

To get the arrays, you just use the following (where the number is the index): 要获取数组,只需使用以下命令(其中数字为索引):

array.get(0); //for an ArrayList
array.get(1);

or 要么

array[0]; //for an array
array[1];

Check out the ArrayList javadoc for more information. 请查看ArrayList javadoc以获取更多信息。

(edit: variable changed to match naming conventions) (编辑:变量已更改以匹配命名约定)

Try this: 尝试这个:

List<char[][]> list = new ArrayList<>();
list.add(ARRAY_1);
list.add(ARRAY_2);

Or 要么

char[][][] ARRAY = new char[length][][];
ARRAY[0] = ARRAY_1;
ARRAY[1] = ARRAY_2;

Or 要么

char[][][] ARRAY = new char[][][]{ARRAY_1, ARRAY_2};

Further reading: 进一步阅读:

So ... if you are not allowed to use lists ... this is one way to make an array of existing arrays. 所以...如果不允许使用列表...这是制作现有数组的一种方法。

 char[][][] ARRAY = new char[][][]{ARRAY_1, ARRAY_2};

Insight #1: an N-dimension array in Java is an array of N-1 dimension arrays (assuming N > 1). 见解1:Java中的N维数组是N-1维数组(假设N> 1)的数组。

Insight #2: arrays are indexed from zero. 洞察#2:数组从零开始索引。


How would I call the arrays individually again later on? 以后如何再次分别调用数组?

  1. You still have the names of the original arrays ... in your example. 在示例中,您仍然具有原始数组的名称。

  2. Base on insight #1": 基于见解1”:

     char[][] ARRAY_1_AGAIN = ARRAYS[0]; System.out.println(ARRAY_1 == ARRAY_1_AGAIN); // prints true 

    Since ARRAY_1 is the first subarray of ARRAY (as per the previous example), we need to use ARRAYS[0] (not ARRAYS[0] ) to access it. 由于ARRAY_1ARRAY的第一个子ARRAY (按照前面的示例),因此我们需要使用ARRAYS[0] (不是ARRAYS[0] )进行访问。

You are using a Jagged Array... 您正在使用锯齿阵列...

Also try this 也试试这个

char[][] array = new char[5][];

array[0] = array1;
array[1] = array2;

Regards 问候

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

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