[英]ArrayList not storing values
I am new to the concept of ArrayLists. 我是ArrayLists概念的新手。 I am using them as I want a dynamic array that isn't limited in the size of how many values it can hold in sequence. 我使用它们是因为我想要一个动态数组,该数组不受顺序可以容纳多少个值的大小限制。 But the method I am using is not storing values correctly. 但是我使用的方法不能正确存储值。 I only keeps one value being the value 1. My code is as follows: 我只保留一个值作为值1。我的代码如下:
public void Rand(){
Random rand = new Random();
int Val = rand.nextInt(5);
ArrayList<Integer> ValList = new ArrayList<Integer>();
ValList.add(Val);
Log.d("LOOK", Integer.toString(ValList));
Log.i("VALUE LIST ", ValList.size()+" <<<<LIST HERE"); }
This process is called in oncreate and then returned to on a loop Ideally every time this class runs a new random number will be added to the array in sequence. 此过程在oncreate中调用,然后循环返回。理想情况下,每次此类运行时,都会将一个新的随机数依次添加到数组中。
This is the relevant info on the logcat: 这是有关logcat的相关信息:
03-05 19:15:15.020: D/LOOK(21325): 3
03-05 19:15:15.020: I/VALUE LIST(20883): 1 <<<<LIST HERE
03-05 19:15:15.040: D/LOOK(21325): 5
03-05 19:15:15.040: I/VALUE LIST(20883): 1 <<<<LIST HERE
As you can see it only stores the value 1 even though the random value is not 1. Am I using the wrong type of array or implementing it incorrectly? 如您所见,即使随机值不是1,它也只存储值1。我使用的数组类型错误还是实现不正确?
The output i'd want to see is something like this: 我想要看到的输出是这样的:
03-05 19:15:15.020: D/LOOK(21325): 3
03-05 19:15:15.020: I/VALUE LIST(20883): 3 <<<<LIST HERE
03-05 19:15:15.040: D/LOOK(21325): 5
03-05 19:15:15.040: I/VALUE LIST(20883): 3, 5 <<<<LIST HERE
Because, Your ArrayList<Integer> ValList = new ArrayList<Integer>();
因为,您的ArrayList<Integer> ValList = new ArrayList<Integer>();
has local scope for Rand()
method only, So its has new instance every time you called Rand()
function. 仅具有Rand()
方法的局部作用域,因此,每次调用Rand()
函数时,它都有一个新实例。
Just declare ArrayList<Integer> ValList = new ArrayList<Integer>();
只需声明ArrayList<Integer> ValList = new ArrayList<Integer>();
as class level scope. 作为班级范围。
And use, 并使用
Like, 喜欢,
// Class level member declaration
ArrayList<Integer> ValList = new ArrayList<Integer>();
public void Rand(){
Random rand = new Random();
int Val = rand.nextInt(5);
ValList.add(Val);
Log.d("LOOK", ValList.toString());
// Also here you are printing size of Arraylist not a content of arraylest
Log.i("VALUE LIST ", ValList.size()+" <<<<LIST HERE");
}
To print specific object value you have to get object from ArrayList using .get()
with position of object, or use .toString()
to print all list contents 要打印特定的对象值,您必须使用带有对象位置的.get()
从ArrayList中获取对象,或者使用.toString()
来打印所有列表内容
Log.i("VALUE LIST ", ValList.toString()+" <<<<LIST items"); // using .toString() to print all contents of List
Update: 更新:
As I have doubt over this line throws Exception (But from your logcat its working fine, as list contains only one item) 我对此行的抛出异常感到怀疑(但是从您的logcat来看,它的工作正常,因为列表仅包含一项)
Log.d("LOOK", Integer.toString(ValList));
As you are trying to print values of list valList
, simply use .toString()
like, ValList.toString()
当您尝试打印列表valList
值时,只需使用.toString()
如ValList.toString()
You are trying to print size of your list. 您正在尝试打印列表的大小。
Log.i("VALUE LIST ", ValList.size()+" <<<<LIST HERE"); }
Log.i("VALUE LIST ", ValList.size()+" <<<<LIST HERE"); }
// wrong (as per ques) Log.i("VALUE LIST ", ValList.size()+" <<<<LIST HERE"); }
//错误(根据问题)
If you want to know what your array contains, try to print the content of your array. 如果您想知道阵列包含的内容,请尝试打印阵列的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.