简体   繁体   English

如何在Java ArrayList中向后拉数据

[英]How to pull data moving backwards in a java ArrayList

I am trying to make it so I can write data to a ArrayList to track random integers and then I will have a previous button that will go back to the last result (This is just testing that code this is not the full code for the project) I am getting an error when trying to take the int arraySize and subtract it by 1 and reprint the data. 我正在尝试使其能够将数据写入ArrayList以跟踪随机整数,然后将具有一个上一个按钮,该按钮将返回到最后一个结果(这只是测试该代码,这不是该项目的完整代码) )尝试获取int arraySize并将其减去1并重新打印数据时,出现错误。 Any idea why this is happening or how another way to accomplish the task. 任何想法,为什么会发生这种情况,或如何以另一种方式完成任务。

int lenth = 10;
ArrayList<Integer> list = new ArrayList<Integer>();
int arraySize = list.size();
Random random = new Random();
int newRandom = random.nextInt(lenth);
list.add(newRandom);
newRandom = random.nextInt(lenth);
list.add(newRandom);
System.out.println(list.get(arraySize));
arraySize--;
System.out.println(list.get(arraySize));

Also here is the error I am getting: 这也是我得到的错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at test.C_MainActivity.main(C_MainActivity.java:22)

When you assign one primitive (such as an int ) to another, this copies the value to the new variable. 当您将一个原语 (例如int )分配给另一个原语时,这会将值复制到新变量中。 For objects, this works a bit differently in that these variables will point to the same object. 对于对象,这有点不同,因为这些变量将指向同一对象。

int arraySize = list.size();

So the above line will just set arraySize to the current size of the list, arraySize will not change as the list size changes. 因此,以上行仅将arraySize设置为列表的当前大小, arraySize不会随着列表大小的变化而改变。 Since you do this when the list is empty, it will be and remain 0 and you'll try to get the elements at indices 0 and 0-1 = -1 , which is where your exception comes in. 由于您在列表为空时执行此操作,因此列表将为0并保持为0 ,您将尝试获取索引为00-1 = -1的元素,这是您的异常所在。

You should just set it after you've inserted all elements instead. 您应该只在插入所有元素之后进行设置。


Note that indexing in Java starts at 0, so the last element will already be at list.size() - 1 , not list.size() . 请注意,Java中的索引从0开始,因此最后一个元素将已经在list.size() - 1 ,而不是list.size()

You are setting arraySize before anything has been added to the list. 在将任何内容添加到列表之前,您要设置arraySize。 The value of arraySize will always be 0 because you only set it at the beginning. arraySize的值将始终为0,因为您仅在开始时进行了设置。 When you call arraySize--, arraySize is now -1 and when you call list.get(arraySize), you will get an index out of bound exception as you are asking for index -1. 当您调用arraySize--时,arraySize现在为-1,而当您调用list.get(arraySize)时,您将在请求索引-1时获得索引超出范围的异常。

It seems like your goal is to enqueue values in a collection and then dequeue them. 看来您的目标是将值放入一个集合中,然后将它们出队。 A LinkedList , for example, would be more appropriate with its push() and pop() methods. 例如, LinkedList与其push()pop()方法更合适。

Anyway you should not keep track of the ArrayList 's size through an external variable. 无论如何,您不应该通过外部变量来跟踪ArrayList的大小。 Such data is already provided for you inside ArrayList so you just have to query the list to know its size. ArrayList已经为您提供了此类数据,因此您只需查询列表即可知道其大小。

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

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