ArrayList不存储值(ArrayList not storing values)

编程入门 行业动态 更新时间:2024-10-20 07:55:14
ArrayList不存储值(ArrayList not storing values)

我是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 HERE

I 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 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?

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 List

Update:

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

更多推荐

本文发布于:2023-07-24 00:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ArrayList   storing   values

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!