如何解决如何从ArrayList的ArrayList中打印字符串
import java.util.*;
import java.lang.*;
import java.io.*;
class Adventurers
{
public static void main (String[] args) throws java.lang.Exception
{
public static void main (String[] args) throws java.lang.Exception{
ArrayList<String> friends = new ArrayList<>(Arrays.asList(new String[]{"Danny","Benni","Marcus","Pat"}));
ArrayList<String> places = new ArrayList<>(Arrays.asList(new String[]{"Paris","Brasil","Miami","Jamaica"}));
ArrayList<String> gifts = new ArrayList<>(Arrays.asList(new String[]{"Snacks","Photos","Instrument","Whine"}));
ArrayList<String> events = new ArrayList<>(Arrays.asList(new String[]{"Zombie Tag ","Bar tour"}));
ArrayList<String> people = new ArrayList<>(Arrays.asList(new String[]{"Karen "," Ryu "," Darth Vader"}));
ArrayList<String> animals = new ArrayList<>(Arrays.asList(new String[]{"Dog "," Cat "," Bird "}));
ArrayList[] objectives = new ArrayList<>(Arrays.asList(new String[] {places,gifts,events,people,animals});
for(String friend : friends){
System.out.println(objectives.get(rand.nextInt(objectives.size())).get(rand.nextInt(this.size)));
}
}
}
}
你好,我正在尝试从主ArrayList的ArrayList打印随机消息,因为我正在尝试打印每个朋友的目标列表(另一个列表)。我想知道如何单独打印它们,尽管我想知道是否可以从主ArrayList打印字符串。
解决方法
您的代码有多个小语法错误。我已经修复了它们:
import java.util.*;
import java.lang.*;
import java.io.*;
public class Adventurers
{
public static void main (String[] args) throws java.lang.Exception{
ArrayList<String> friends = new ArrayList<>(Arrays.asList(new String[]{"Danny","Benni","Marcus","Pat"}));
ArrayList<String> places = new ArrayList<>(Arrays.asList(new String[]{"Paris","Brasil","Miami","Jamaica"}));
ArrayList<String> gifts = new ArrayList<>(Arrays.asList(new String[]{"Snacks","Photos","Instrument","Whine"}));
ArrayList<String> events = new ArrayList<>(Arrays.asList(new String[]{"Zombie Tag ","Bar tour"}));
ArrayList<String> people = new ArrayList<>(Arrays.asList(new String[]{"Karen "," Ryu "," Darth Vader"}));
ArrayList<String> animals = new ArrayList<>(Arrays.asList(new String[]{"Dog "," Cat "," Bird "}));
ArrayList[] objectives = new ArrayList<>(Arrays.asList(new String[] {places,gifts,events,people,animals}));
for(String friend : friends){
System.out.println(objectives.get(rand.nextInt(objectives.size())).get(rand.nextInt(this.size)));
}
}
}
然后您可以参考this question来解决问题。
,您正在使用什么版本的Java,从版本8开始,您可以使用以下流:
String value = friends.stream().flatMap(e -> e.stream()).findAny().get();
,
您的代码有多个小语法错误。我认为您需要了解更多有关Java的知识。
public static void main (String[] args){
Random rand = new Random();
ArrayList<String> friends = new ArrayList<>(Arrays.asList("Danny","Pat"));
ArrayList<String> places = new ArrayList<>(Arrays.asList("Paris","Jamaica"));
ArrayList<String> gifts = new ArrayList<>(Arrays.asList("Snacks","Whine"));
ArrayList<String> events = new ArrayList<>(Arrays.asList("Zombie Tag ","Bar tour"));
ArrayList<String> people = new ArrayList<>(Arrays.asList("Karen "," Darth Vader"));
ArrayList<String> animals = new ArrayList<>(Arrays.asList("Dog "," Bird "));
List[] objectives = new List[]{Arrays.asList(places,animals)};
for(String friend : friends){
System.out.println(objectives[rand.nextInt(objectives.length)].get(rand.nextInt(friend.length())));
}
}
,
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rand = new Random();
List<String> friends = Arrays.asList("Danny","Pat");
List<String> places = Arrays.asList("Paris","Jamaica");
List<String> gifts = Arrays.asList("Snacks","Whine");
List<String> events = Arrays.asList("Zombie Tag","Bar tour");
List<String> people = Arrays.asList("Karen","Ryu","Darth Vader");
List<String> animals = Arrays.asList("Dog","Cat"," Bird");
List<List<String>> lists = Arrays.asList(places,animals);
for (String friend : friends) {
System.out.print(friend + ": ");
boolean isFirst = false;
for (List<String> list : lists) {
if (!isFirst) {
isFirst = true;
} else {
System.out.print(",");
}
System.out.print(list.get(rand.nextInt(list.size())));
}
System.out.println();
}
}
}
老实说,我花了更多时间弄清楚您的初衷。经过大量的猜测之后,我认为这就是您想要的。
存在一些语法错误和一些不必要的嵌套函数调用,我为您删除了它们。请仔细研究差异。
此外,我认为我们都需要了解一些内容:如果需要使用某个内置对象,请先弄清楚其内置方法;如果要调用内置方法,请先了解其参数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。