Array of string lists

  • 6
  • Locked
Sometimes a task is just a task. With no dirty tricks, and no need to look for complexity or depth. You just need to sit down and do it. Just such a task is before us. See how everything is clear and simple: create an array whose elements are lists of strings. And then fill the array with any data and display it on the screen.
You can't complete this task, because you're not signed in.
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Max Sudik
Level 41 , Vancouver, Canada
30 September 2021, 21:50
In the provided solution the ArrayList was declared like this:
ArrayList<String>[] result = (ArrayList<String>[]) new ArrayList[3];
In my case I declared as this and everything worked:
ArrayList<String>[] result = new ArrayList[3];
Could you explain me why do we need or don't need to cast here? Thanks, M
Jomar Tayactac
Level 27 , Winnipeg, Canada
6 December 2021, 23:14
Curious about this as well. Did you find your answer?
Elektra
Level 15 , United States of America, United States
26 January, 19:01
Thank you so much for sharing this. I wasted so much time trying to figure out how to fix the error until I read your post. I was getting a "Generic Array Creation" error. When I tried the 2 suggestions in your posts. They both worked. I do not completely understand why, but after some research I found that when you're declaring an array of a "generic type" you have to cast it. Maybe that's how the compiler sees the ArrayList<String> when undeclared yet. Technically, it still didn't have any data or size or has been allocated any memory for it. Maybe what you did by omitting the <String> part was just hide the portion that was confusing the compiler until it fills up the gap later when you declare the actual content. I don't know if that makes sense or not, but if someone has a better understanding I would really appreciate a good explanation as well.
Cam
Level 15 , Bogota, Colombia
10 November 2020, 20:27
it took me a good couple of hours exploring different versions of the code to get to a point where I was comfortable verifying. I still don't feel like I get the full picture of these kinds of algorithms. But if I had a tip for any one struggling with this task, it would be to be careful with how and when you initialize the arrays and the lists. Also play around with how data flows inside nested for loops