Gommeh
Veteran Member
dammit ryan and rylie
Posts: 2,744
| Likes: 778
|
Post by Gommeh on Apr 13, 2019 21:24:06 GMT
I have a project for my computer science class due this week and I need to create an array of ArrayLists, but am not sure how. My code so far:
ArrayList<String>[] reports = new ArrayList<String>[19]; // Error here: "Cannot create a generic array of ArrayList<String>" // ... unrelated code for (int i = 0; i < 19; i++) { // total of 19 employees String line = inputFile.nextLine(); Scanner s = new Scanner(line).useDelimiter(";");
String temp = s.next(); // this is a set of 6-digit numbers separated by spaces, eg: "123456 654321"
String[] rep = temp.split(" "); // splits up the rest of the line between spaces reports[i] = new ArrayList<>(); for (int k = 0; k < rep.length; k++) { reports[i].add(rep[k]); // adds rep[k] into the arrayList "reports[i]" } }
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Apr 13, 2019 21:49:51 GMT
I think you need to use Integers? Or put the 19 in quotes so that it registers that as a string?
List<String> REPORTS = Arrays.asList("Your", "Strings", "Here");
That should work, and I think you can just replace <String> with <Integer> if you're looking to just use numbers. Untested so not sure.
|
|
StevenNL2000
Forum Admin
Posts: 6,415
| Likes: 6,936
IGN: StevenNL2000
Timezone: UTC+01:00
Member is Staff. Need immediate assistance? Send a PM
|
Post by StevenNL2000 on Apr 13, 2019 22:33:20 GMT
You are running into a conceptual problem with arrays, namely the inference of the type parameter. Essentially, an array can guarantee that it will only contain instances of ArrayList, but it cannot guarantee that all of those instances of ArrayList will have <String> as their type parameter, even though mixing them like that is not allowed.ArrayList<String>[] array1 = new ArrayList<String>[1]; // Error: "Cannot create a generic array of ArrayList<String>" ArrayList<String>[] array2 = new ArrayList[1]; // Warning: "Type safety: The expression of type ArrayList[] needs unchecked conversion to conform to ArrayList<String>[]" ArrayList[] array3 = array1; // Warning: "ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized" ArrayList[] array4 = array2; // Warning: "ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized" array1[0] = new ArrayList<Integer>(); // Error: "Type mismatch: cannot convert from ArrayList<Integer> to ArrayList<String>" array2[0] = new ArrayList<Integer>(); // Error: "Type mismatch: cannot convert from ArrayList<Integer> to ArrayList<String>" array3[0] = new ArrayList<Integer>(); // No warning! array4[0] = new ArrayList<Integer>(); // No warning! The only way to continue with an array of ArrayLists (an ArrayList of ArrayLists is much better) is to use the method of array2, and make sure that you absolutely never do anything like array4.
|
|
super
Veteran Member
Among Us
Posts: 1,282
|
Post by super on Apr 14, 2019 16:00:16 GMT
ooh java for school, neato
|
|
Lemon
Veteran Member
Insert status here.
Posts: 2,491
| Likes: 2,165
|
Post by Lemon on Apr 14, 2019 16:32:06 GMT
I'd use a HashMap instead:
HashMap<Integer, ArrayList<String>> employees = new HashMap<>(); // ... unrelated code for (int i = 0; i < 19; i++) { // total of 19 employees String line = inputFile.nextLine(); Scanner s = new Scanner(line).useDelimiter(";"); String temp = s.next(); // this is a set of 6-digit numbers separated by spaces, eg: "123456 654321" String[] rep = temp.split(" "); // splits up the rest of the line between spaces ArrayList<String> array = employees.getOrDefault(i, new ArrayList<String>()); for (int k = 0; k < rep.length; k++) { array.add(rep[k]); } employees.put(array); }
when you want to get:
for(int i : employees.keySet()) { ArrayList<String> array = employees.get(i); //do something }
|
|
Gommeh
Veteran Member
dammit ryan and rylie
Posts: 2,744
| Likes: 778
|
Post by Gommeh on Apr 15, 2019 14:58:50 GMT
I just did this instead:
// note to self: when making an array of ArrayLists, always make sure to use this method ArrayList<String>[] reports = new ArrayList[19]; // notice no "<String>" here // ... unrelated code
// read the data for (int i = 0; i < 19; i++) { // total of 19 employees String line = inputFile.nextLine(); Scanner s = new Scanner(line).useDelimiter(";"); String temp = s.next(); String[] rep = temp.split(" "); // splits up the rest of the line between spaces reports[i] = new ArrayList<>(); for (int k = 0; k < rep.length; k++) { // adds rep[k] into the arrayList reports[i] reports[i].add(rep[k]); }
|
|