简体   繁体   English

在 Java 中,如何访问整数数组的 ArrayList

[英]In Java, how can I access an ArrayList of integer arrays

Here's my code:这是我的代码:

    // create an arraylist of type integer array
    ArrayList<int[]> timesTables = new ArrayList<>();

    // add data
    timesTables.add(new int[]{42, 17, 81});
    timesTables.add(new int[]{1, 2, 3});
    timesTables.add(new int[]{1000, 2000, 3000});

    // this does not work        
    Log.i("Status ", timesTables.get((0)[0]).toString());

I know the mistake here has something to do with the way I'm attempting to reference the data with (0)[0], but I can't figure out the correct syntax.我知道这里的错误与我尝试使用 (0)[0] 引用数据的方式有关,但我无法弄清楚正确的语法。 I know that this works:我知道这有效:

    Log.i("Status ", timesTables.get(0).toString());

But this only gives the array address (I think) and not the values of an array nor an individual value in one array, which is what I'm attempting.但这仅给出数组地址(我认为),而不是数组的值,也不是一个数组中的单个值,这就是我正在尝试的。

Secondary question: I attempted to look at the documentation to answer this question myself, but as a beginner, I was unsure where to look.次要问题:我试图查看文档来自己回答这个问题,但作为初学者,我不确定在哪里看。 I didn't know which website I should be using or which piece of code I should be looking up (get, int[], etc..).我不知道我应该使用哪个网站或我应该查找哪一段代码(get、int[] 等)。

Thanks in advance for your help.在此先感谢您的帮助。

What you are creating is a list of int primitive arrays .您正在创建的是一个int 原始数组列表

So, use the List's get() method to obtain by index which array to use, followed by the index of the target entry in the obtained array.因此,使用 List 的get()方法通过索引获取要使用的数组,然后是获取的数组中目标条目的索引。

timesTables.get(0)[0]

You can't use toString as the value you wish to print is an int primitive type .您不能使用toString作为您希望打印的值是int 原始类型

So for your specific question you can do as next:因此,对于您的具体问题,您可以执行以下操作:

 Log.i("Status", Integer.toString(timesTables.get(0)[0]));

To clarify a bit your doubts, in a more verbose way, the above is the same as doing next:为了澄清一点你的疑问,更详细地说,上面的和下面的一样:

 int[] array = timesTables.get(0); // Get by index an array from the list
 int value = array[0]; // Get by index an int value from the obtained array
 Log.i("Status", Integer.toString(value));

You can't put an index of nothing, so you should write the name of the array you're trying to access when you write (0) .您不能放置空索引,因此您应该在写入(0)时写入要访问的数组的名称。

// create an arraylist of type integer array
    ArrayList<int[]> timesTables = new ArrayList<>();

    // add data
    timesTables.add(new int[]{42, 17, 81});
    timesTables.add(new int[]{1, 2, 3});
    timesTables.add(new int[]{1000, 2000, 3000});

    // this does not work        
    Log.i("Status ", Integer.toString(timesTables.get(0)[0]));

You are correct in that this prints the object reference of the int array.您是正确的,因为这会打印 int 数组的对象引用。

Log.i("Status ", timesTables.get(0).toString());

If you want to print the contents of the array instead, use Arrays.toString(...) , eg:如果要打印数组的内容,请使用Arrays.toString(...) ,例如:

Log.i("Status ", Arrays.toString(timesTables.get(0)));

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

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