-4

I have created an ArrayList from an Array. I want to print one random item from this ArrayList.

How do I do this? I have inserted the code.

public class Song {

    final String [] songArray = {
        "song1", 
        "song2"
    };

    ArrayList<String> songList = new ArrayList<>(Arrays.asList(songArray));

}
Zaki Hussain
  • 27
  • 1
  • 6
  • 7
    `songList.get(new Random().nextInt(songList.size()))` – Arnaud Denoyelle Nov 09 '17 at 16:48
  • You probably meant to do: `List songList = Arrays.asList(songArray);` your code is currently creating *two* lists, the first with `Arrays.asList(songArray)` and then it creates a second one which copies the items from the first one with: `new ArrayList<>(...)` – Nir Alfasi Nov 09 '17 at 16:48
  • @alfasin That won't compile so I doubt that that's what he meant. Now after you changed it, it will compile but there are still many legitimate reasons to create a new list with `new ArrayList<>`. `Arrays.asList` is a very cheap operation which servers as a connector between array and list. – Oleg Nov 09 '17 at 16:50
  • @alfasin note that `Arrays.asList(...)` returns a `java.util.Arrays.ArrayList` which is a private static class, so that code wouldn't work. It should be `List songList ...` anyways. – Thomas Nov 09 '17 at 16:50
  • 1
    @alfasin If you use only Arrays.asList() you won't be able to add new elements, with new ArrayList<>(Arrays.asList()) you'll be able to ;) depend what you need – azro Nov 09 '17 at 16:55
  • @alfasin I think `List songList = new ArrayList() {{add("song1");add("song2");} };` would be the one you are trying to say. – Shubhendu Pramanik Nov 09 '17 at 17:06
  • @ShubhenduPramanik nope, what I wrote is exactly what I meant. This code doesn't require modifying the list after it was created, just to print its elements. – Nir Alfasi Nov 10 '17 at 01:24

1 Answers1

2

You can try this.

    List<String> list = new ArrayList<>();
    list.add("apple");
    list.add("banana");
    Random random = new Random();

    System.out.println(list.get(random.nextInt(list.size())));
Ben
  • 196
  • 15
  • I have tried this, but this does not work. – Zaki Hussain Nov 09 '17 at 16:58
  • 1
    @ZakiHussain "does not work" is rarely sufficient, you need to specify in what way it doesn't work (e.g. what did you expect and what did you get instead?). – Thomas Nov 09 '17 at 16:59
  • list.add ... doesn't work. It says illegal start of type – Zaki Hussain Nov 09 '17 at 17:08
  • 1
    post your code here. You have to have right type of ArrayList. `List ...` – Ben Nov 09 '17 at 17:11
  • `ArrayList songList = new ArrayList<>(); songList.add("song1"); songList.add("song2"); Random random = new Random(); System.out.println(list.get(random.nextInt(list.size())));` – Zaki Hussain Nov 09 '17 at 17:14
  • 1
    System.out.println(songList.get(random.nextInt(songList.size()))); insteat of System.out.println(list.get(random.nextInt(list.size()))); – Raul Vasi Nov 09 '17 at 17:15
  • `System.out.println(list.get(random.nextInt(list.size())));` should be `System.out.println(songList.get(random.nextInt(songList.size())));` just rename the variable. Your code works fine. – Ben Nov 09 '17 at 17:18
  • If you don't have any problems, mark this question as the best answer and close this topic. Thanks. – Ben Nov 09 '17 at 17:33