如何洗牌列表
我需要一些帮助,以编写一种可以洗牌列表的方法。我无法弄清楚在我的方法中放置什么。这是我到目前为止所拥有的。我尝试使用随机方法将列表中的整数随机化,但这无效。有人可以告诉我如何做吗?
这是我尝试过的代码:
import java.util.ArrayList;
import java.util.Scanner;
public class Lab 11 {
public static void main(String[] args) {
ArrayList<Double> list = new ArrayList<Double>();
Scanner input = new Scanner(System.in);
System.out.print("Enter integers (input ends with 0): ");
double value;
do {
value = input.nextDouble(); // Read a value from the input
if (value != 0)
list.add(value); // Add the value if it is not in the list
} while (value != 0);
System.out.println("The maximum number is " + max(list));
System.out.print("Enter five double values: ");
for (int i = 0; i < 5; i++)
list.add(input.nextDouble());
System.out.println("The sum is " + sum(list));
}
public static Double max(ArrayList<Double> list) {
if (list == null || list.size() == 0)
return null;
double result = list.get(0);
for (int i = 1; i < list.size(); i++)
if (result < list.get(i))
result = list.get(i);
return result;
}
public static double sum(ArrayList<Double> list) {
double sum = 0;
for (int i = 0; i < list.size(); i++)
sum += list.get(i);
return sum;
}
}
I need some help in writing a method that will shuffle the ArrayList. I can't figure out what to place in my method. Here is what I have so far. I tried using the random method to randomize the integers in the list but that didn't work. Can someone show me how to do this?
Here is the code I've tried:
import java.util.ArrayList;
import java.util.Scanner;
public class Lab 11 {
public static void main(String[] args) {
ArrayList<Double> list = new ArrayList<Double>();
Scanner input = new Scanner(System.in);
System.out.print("Enter integers (input ends with 0): ");
double value;
do {
value = input.nextDouble(); // Read a value from the input
if (value != 0)
list.add(value); // Add the value if it is not in the list
} while (value != 0);
System.out.println("The maximum number is " + max(list));
System.out.print("Enter five double values: ");
for (int i = 0; i < 5; i++)
list.add(input.nextDouble());
System.out.println("The sum is " + sum(list));
}
public static Double max(ArrayList<Double> list) {
if (list == null || list.size() == 0)
return null;
double result = list.get(0);
for (int i = 1; i < list.size(); i++)
if (result < list.get(i))
result = list.get(i);
return result;
}
public static double sum(ArrayList<Double> list) {
double sum = 0;
for (int i = 0; i < list.size(); i++)
sum += list.get(i);
return sum;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此方法并将您的数组传递到参数中,
此方法返回void,因此它不会给您一个新列表,但是正如我们所知道的那样,数组是作为Java中的参考类型传递的,因此它将洗牌您的数组并在其中保存休息值。这就是为什么您不需要任何返回类型的原因。
您现在可以使用被改组的arraylist。
Use this method and pass your array in parameter
This method return void so it will not give you a new list but as we know that array is passed as a reference type in Java so it will shuffle your array and save shuffled values in it. That's why you don't need any return type.
You can now use arraylist which is shuffled.
尝试 collections.shuffle(list)< /a>。如果禁止使用此方法解决问题,则可以查看实际的实现。
Try Collections.shuffle(list).If usage of this method is barred for solving the problem, then one can look at the actual implementation.