我是ArrayLists概念的新手。 我正在使用它们,因为我想要一个动态数组,其数量不限于它可以按顺序保存多少个值。 但我使用的方法不是正确存储值。 我只保留一个值为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"); }此过程在oncreate中调用,然后返回到循环中。理想情况下,每次运行此类时,新的随机数将按顺序添加到数组中。
这是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正如您所看到的,即使随机值不是1,它也只存储值1.我使用了错误的数组类型还是错误地实现了它?
我希望看到的输出是这样的:
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 HEREI am new to the concept of 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:
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.
This is the relevant info on the 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 HEREAs 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?
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最满意答案
因为,你的ArrayList<Integer> ValList = new ArrayList<Integer>(); 仅具有Rand()方法的局部范围,因此每次调用Rand()函数时它都有新实例。
只需声明ArrayList<Integer> ValList = new ArrayList<Integer>(); 作为班级范围。
并使用,
喜欢,
// 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"); }要打印特定的对象值,您必须使用带有位置的.get()从ArrayList获取对象,或使用.toString()打印所有列表内容
Log.i("VALUE LIST ", ValList.toString()+" <<<<LIST items"); // using .toString() to print all contents of List更新:
因为我怀疑这行抛出异常(但从你的logcat它的工作正常,因为列表只包含一个项目)
Log.d("LOOK", Integer.toString(ValList));当您尝试打印列表valList值时,只需使用.toString()如ValList.toString()
Because, Your ArrayList<Integer> ValList = new ArrayList<Integer>(); has local scope for Rand() method only, So its has new instance every time you called Rand() function.
Just declare 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
Log.i("VALUE LIST ", ValList.toString()+" <<<<LIST items"); // using .toString() to print all contents of ListUpdate:
As I have doubt over this line throws Exception (But from your logcat its working fine, as list contains only one item)
Log.d("LOOK", Integer.toString(ValList));As you are trying to print values of list valList, simply use .toString() like, ValList.toString()
更多推荐
发布评论