[英]How to print a string from an ArrayList of an 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)));
}
}
}
然后你可以參考這個問題來解決這個問題。
您使用的是什么版本的 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", "Benni", "Marcus", "Pat"));
ArrayList<String> places = new ArrayList<>(Arrays.asList("Paris", "Brasil", "Miami", "Jamaica"));
ArrayList<String> gifts = new ArrayList<>(Arrays.asList("Snacks", "Photos", "Instrument", "Whine"));
ArrayList<String> events = new ArrayList<>(Arrays.asList("Zombie Tag ", "Bar tour"));
ArrayList<String> people = new ArrayList<>(Arrays.asList("Karen ", " Ryu ", " Darth Vader"));
ArrayList<String> animals = new ArrayList<>(Arrays.asList("Dog ", " Cat ", " Bird "));
List[] objectives = new List[]{Arrays.asList(places, gifts, events, people, 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", "Benni", "Marcus", "Pat");
List<String> places = Arrays.asList("Paris", "Brasil", "Miami", "Jamaica");
List<String> gifts = Arrays.asList("Snacks", "Photos", "Instrument", "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, gifts, events, people, 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();
}
}
}
老實說,我花了更多的時間來弄清楚你最初的目的。 經過大量的瘋狂猜測,我認為這就是您想要的。
有一些語法錯誤和一些不必要的嵌套函數調用,我為您刪除它們。 請仔細查看差異。
另外,我覺得有一點我們都需要明白:如果需要使用某個內置對象,首先要弄清楚它的內置方法; 如果要調用一個內置方法,首先要知道它的參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.