Arraylist Problem | Sololearn: Learn to code for FREE!


Arraylist Problem

import java.util.ArrayList; import java.util.List; import java.util.Random; public class Funktion { public static ArrayList<String> Sitzplatz = new ArrayList<String>(); //public static String[] Sitzplatz = new String[s]; public static ArrayList<String> List = new ArrayList<String>(); static String a = "Mirko und Sarah"; static String b = "Mats und Stefan"; static String c = "Elena und Gina"; static String d = "Melina und Beatrice"; static String e = "Jelva und Nele"; static String f = "Lasse-Bo und Erik"; static String g = "Daniel und Raphael"; static String h = "Lorenz und Basti"; static String i = "Anton und Marius"; static String j = "Clas und Jan"; public Funktion() { Random random = new Random(); List<String> Sp = new ArrayList<String>(); Sp.add(a); Sp.add(b); Sp.add(c); Sp.add(d); Sp.add(e); Sp.add(f); Sp.add(g); Sp.add(h); Sp.add(i); Sp.add(j); List.add("Erste Reihe Rechts"); List.add("Erste Reihe Links"); List.add("Zweite Reihe Rechts"); List.add("Zweite Reihe Mitte"); List.add("Zweite Reihe Links"); List.add("Dritte Reihe Rechts"); List.add("Dritte Reihe Mitte"); List.add("Dritte Reihe Links"); List.add("Vierte Reihe Rechts"); List.add("Vierte Reihe Links"); for (int i = 0; i < Sitzplatz.size(); i++) { int Index = random.nextInt(Sp.size()); Sitzplatz[i] = Sp.get(Index); Sp.remove(Index); } I want to use this Code with an ArrayList,because i want to upgrade the ArrayList, but this Code only works with a normal Array. Can someone please help me to find the right Code. The error is by Sitzplan[i] = Sp.get(index)

4/28/2018 7:55:43 PM

Stefan Mueller

2 Answers

New Answer


A better approach is by turning taking all those string variables and get them into an array, and then you take that array of strings into an ArrayList. 1. import the "Arrays" class. 2. declare an Array of Strings "myStrings" and get all those String values there, eliminaring all those variables. 3. declare and ArrayList, but instantiate it with the "myStrings Array. example: ArrayList myStringList = Arrays.asList (myStrings); There you have a normal array, and a way to turn it into an arraylist. Hope this is what you wanted.


Ok thank you. I understood it after I searched a Little bit on the Internet. I am new to Java