0
public static void main (String[] args) throws java.lang.Exception{
    ArrayList friends = new ArrayList<>(asList("Danny", "Benni", "Marcus", "Pat"));
    ArrayList Places = new ArrayList<>(asList("Paris", "Brasil", "Miami", "Jamaica"));
    ArrayList Gifts = new ArrayList<>(asList("Snacks", "Photos", "Instrument", "Whine"));
    
    ArrayList[] travelgoals = new ArrayList<>{Places, Gifts};
    
    for (int b = 0; b > friends.length; b++){
        if(b > Places.length) {
            System.out.println(Places.get().Random((0), Places.length);
        }if (b > Gifts.length) {
            System.out.println(Gifts.get().Random((0), Gifts.length);
        }
    }else {
        System.out.println("Looks like you got off scottfree!");
    }
}

Hello I'm trying to use string arrays to print random goals in locations though I think I'm more than way off.

7
  • How did you decide to call .Random() on the result of that .get() ?? Commented Sep 10, 2020 at 21:25
  • Java naming conventions have methods and variables start with lower case letters. Commented Sep 10, 2020 at 21:34
  • @azro my logic was to use .Random() to randomly choose an entry from that array. My goal would be to print a friends name along with a random place and random gift Commented Sep 10, 2020 at 21:43
  • @NomadMaker You are correct. I kinda forgot that as I was working with strings and wanted to handle this all in one method. Commented Sep 10, 2020 at 21:44
  • I got it , but why do you think you a .Random() method exists, because it just not exists Commented Sep 11, 2020 at 6:04

1 Answer 1

1

The property you're looking for is "size".

ArrayList and arrays are different here in this aspect.You get the length of an array with "arr.length" and the length(or size) of an ArrayList with "arrList.size()".

Also to generate random index within the range of size you can use Random class's nextInt method.

Below is the code similar to what you're trying to do which picks random gifts and places for each friend.

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"}));
        
    Random rand = new Random();//This is the Random class that can be used to generate random number. In this case Integers
    

    //just a for loop iterating through all friends
    //This will pick a random element from each of places and gifts
    //You can get an element of a list by using the get 
    //method(arrList.get(index))

    for(String friend : friends){
        System.out.println(places.get(rand.nextInt(places.size())));
        System.out.println(gifts.get(rand.nextInt(gifts.size())));
     }
}

Edit : Added a more scalable way using Map since if you keep on increasing properties it'll be hectic to maintain.

Map<String, ArrayList<String>> mapOfProperties = new HashMap<>();

mapOfProperties.put("friends",Arrays.asList(new String[]{"Danny", "Benni", "Marcus", "Pat"}));

mapOfProperties.put("places",Arrays.asList(Arrays.asList(new String[]{"Paris", "Brasil", "Miami", "Jamaica"}));

mapOfProperties.put("gifts",Arrays.asList(new String[]{"Snacks", "Photos", "Instrument", "Whine"}));

Set<String> keysOfMap = mapOfProperties.keySet(); 

Random rand = new Random();

for(int i=0;i<map.get("friends").size();i++){
    for(String keyName : keysOfMap){
       int keyIndex = rand.nextInt(mapOfProperties.get(keyName).size());
       //can put mapOfProperties.get(keyName) in a temp arrayList as well
       System.out.print(mapOfProperties.get(keyName).get(keyIndex));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this! I'm use to using .length so I tend to forget about .size though I wanted to know if this method would scale or is there away to make this code run shorter. Like if I added another arraylist or so would it be possible to make random calls from a master arrayList? ` ArrayList[] objectives = new ArrayList<>(Arrays.asList(new String[] {places, gifts, events, people, animals}); `
In terms of complexity. Fetching an element from arraylist with index and gettting the length of a list is constant time operation(O(1)). If you want scalability, I suggest you may go for a HashMap of arraylist with the keys being the properties you want to fetch randomly as you can get any list from the "master" hashmap in constant time. As seen in the code I have updated, you can use the keySet() method to get the list of keys and the get each random property off of each List.This is a more generalized approach.

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.