1


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.

1
  • You don't have "an ArrayList of an ArrayList" anywhere in that code. Please try again. Commented Sep 11, 2020 at 4:00

4 Answers 4

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

What version of java are you using, starting version 8 you can use streams as below:

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

4 Comments

I think you've ignored the most prominent errors in the question - the syntax.
Streams? I have yet to use streams so I'll have to look more into it.
I have not gone by the syntax, instead have answered how to get nested data
@FallenUser you are correct since normally I figure out what I'm trying to do then attempt to figure the syntax that lets me get what I want
0

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

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.