在Java中,我如何访问一个整数数组的ArrayList。

huangapple go评论87阅读模式
英文:

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

问题

这是我的代码:

    // 创建一个整数数组类型的ArrayList
    ArrayList<int[]> timesTables = new ArrayList<>();

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

    // 这部分不起作用        
    Log.i("状态 ", timesTables.get(0)[0] + "");

我知道这里的错误与我尝试使用 (0)[0] 引用数据的方式有关,但我无法找出正确的语法。我知道这个是有效的:

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

但这只会返回数组的地址(我想是这样),而不是数组的值或者一个数组中的单个值,这正是我想要的。

附加问题:我尝试查阅文档以自己找到答案,但作为一个初学者,我不确定应该在哪里查找。我不知道应该使用哪个网站,或者应该查找哪个代码片段(get、int[]等)。

提前感谢你的帮助。

英文:

Here's my code:

    // create an arraylist of type integer array
    ArrayList&lt;int[]&gt; timesTables = new ArrayList&lt;&gt;();

    // 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(&quot;Status &quot;, 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. I know that this works:

    Log.i(&quot;Status &quot;, 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..).

Thanks in advance for your help.

答案1

得分: 1

你正在创建一个由int原始数组组成的列表

因此,使用List的**get()**方法按索引获取要使用的数组,然后使用所获取数组中目标条目的索引。

timesTables.get(0)[0]

由于要打印的值是int原始类型,不能使用toString

因此,对于您的具体问题,您可以像下面这样做:

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

为了更清楚地解释你的疑问,更详细地说,上面的代码与以下操作相同:

int[] array = timesTables.get(0); // 从列表中按索引获取一个数组
int value = array[0]; // 从所获取的数组中按索引获取一个int值
Log.i("Status", Integer.toString(value));
英文:

What you are creating is a list of int primitive arrays.

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.

timesTables.get(0)[0]

You can't use toString as the value you wish to print is an int primitive type.

So for your specific question you can do as next:

 Log.i(&quot;Status&quot;, 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(&quot;Status&quot;, Integer.toString(value));

答案2

得分: -1

你不能给一个空的索引,所以在写(0)时,你应该写上你要访问的数组的名称。

// 创建一个整型数组的ArrayList
ArrayList<int[]> timesTables = new ArrayList<>();

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

// 这部分不起作用
Log.i("状态 ", Integer.toString(timesTables.get(0)[0]));
英文:

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).

// create an arraylist of type integer array
    ArrayList&lt;int[]&gt; timesTables = new ArrayList&lt;&gt;();

    // 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(&quot;Status &quot;, Integer.toString(timesTables.get(0)[0]));

答案3

得分: -1

你在这一点上是正确的,它打印了整型数组的对象引用。

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

如果你想打印数组的内容,可以使用 Arrays.toString(...),例如:

Log.i("状态", Arrays.toString(timesTables.get(0)));
英文:

You are correct in that this prints the object reference of the int array.

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

If you want to print the contents of the array instead, use Arrays.toString(...), e.g.:

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

huangapple
  • 本文由 发表于 2020年10月4日 02:54:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64187873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定