从数组(Array)切换到动态数组(ArrayList)

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

Switching from Array to ArrayList

问题

我有一些代码,我想将其转换为使用ArrayList。

如何在数组中随机生成数字?

当我使用数组时,我这样做:

public void generateNumbers() {
    Random rand = new Random();
    
    for (int i = 0; i < numbersArray.length; i++) {
        numbersArray[i] = rand.nextInt((50 - 1) + 1) + 1;
    }
}

在ArrayList中是否有类似于array[i]的等效写法?

英文:

I have some code that I want to convert over to using an an arraylist.

How would I go about generating numbers randomly in the array

When i used an array i did:

public void generateNumbers() {

		Random rand = new Random();

		for (int i = 0; i &lt; numbersArray.length; i++ ) {
			numbersArray[i] = rand.nextInt((50 - 1) + 1) + 1;
		}	

	} //generateNumbers()

Is there an equivalent of array[i] in ArrayList?

答案1

得分: 2

以下是翻译后的内容:

您可以使用 Stream API 生成随机值的数组或列表:

public static List<Integer> getRandomList(int size, int maxValue) {

    Random random = new Random();
    return IntStream.range(0, size)
                    .map(x -> 1 + random.nextInt(maxValue))
                    .boxed()
                    .collect(Collectors.toList());
}

类似地,也可以生成数组:

public static int[] getRandomArray(int size, int maxValue) {

    Random random = new Random();
    return IntStream.range(0, size)
                    .map(x -> 1 + random.nextInt(maxValue))
                    .toArray();
}
英文:

You can use Stream API to generate array or list of random values:

public static List&lt;Integer&gt; getRandomList(int size, int maxValue) {

    Random random = new Random();
    return IntStream.range(0, size)
                    .map(x -&gt; 1 + random.nextInt(maxValue))
                    .boxed()
                    .collect(Collectors.toList());
}

Similarly, an array can be generated:

public static int[] getRandomArray(int size, int maxValue) {

    Random random = new Random();
    return IntStream.range(0, size)
                    .map(x -&gt; 1 + random.nextInt(maxValue))
                    .toArray();
}

答案2

得分: 1

        随机数生成器 rand = new 随机数生成器();
        整数 listSize = 5;
        列表<整数> numbersList = 新列表<整数>(5);
        对于 (整数 i = 0; i < listSize; i++) {
            numbersList.添加(rand.生成下一个整数());
        }

数组和列表的工作方式不同数组具有静态大小而列表会随着每个添加的项增加其大小
英文:
    Random rand = new Random();
    int listSize = 5;
    List&lt;Integer&gt; numbersList = new ArrayList&lt;&gt;(5)
    for (int i = 0; i &lt; listSize; i++ ) {
        numbersList.add(rand.nextInt());
    }   

Arrays and Lists work differently. Arrays have a static size while lists increase their size with each item added.

答案3

得分: 0

不清楚你的问题是什么,所以我将回答你最后提出的直接问题:

> 在ArrayList中是否有类似于array[i]的等价操作?

这取决于你对它的使用。你可以使用 nameOfArrayList.set(i, *value*) 来设置值,使用 nameOfArrayList.get(i) 来获取值。ArrayList 类中还有许多其他方法。我强烈建议你阅读文档

你还需要注意,ArrayList 与数组的工作方式不同。前者是可变的,而后者不是。你可以在这里阅读相关信息。

英文:

It's not immediately clear what your question is, so I will address the the direct question you asked in the end:

> Is there an equivalent of array[i] in ArrayList?

It depends on what you're doing with it. You would use nameOfArrayList.set(i, *value*) to set the value, and nameOfArrayList.get(i) to retrieve the value. There are a lot more methods in the ArrayList class. I strongly suggest that you read the documentation.

You also need to note that ArrayLists do not work like arrays. The former are mutable while the latter are not. You can read up on it here.

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

发表评论

匿名网友

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

确定