简体   繁体   中英

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)));
        
     }
}
    }
}

Hello I'm trying to print a random message from an ArrayList of a master ArrayList as I'm trying to print a list of objectives per friend (another list). I figured out how to print them separately though I wanted to know if it was possible to print strings from a master ArrayList.

Your code has multiple small syntax errors. I've fixed them:

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)));
        
     }
}
}

You can then refer to this question to solve the issue.

您使用的是什么版本的 java,从版本 8 开始,您可以使用如下流:

String value = friends.stream().flatMap(e -> e.stream()).findAny().get();

Your code has multiple small syntax errors.I think you need learn more knowledge of 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();
        }
    }
}

Honestly, it cost me more time to figure out your original purpose. After a lot of wild guesses, I think this is what you want.

There're some syntax errors and some unnecessary nested function calls, I remove them for you. Please carefully look into the difference.

Also, I think there's something we all need to understand: if a certain built-in Object needs to be used, figure out its built-in methods first; if a built-in method will be called, know its parameters first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM