0

In a given homework problem I'm supposed to create a matrix/or two-dimensional array of 2x9 dimensions in which each element contains an arraylist of objects of "Patient" type.

Patient is an object created from the class Patients.

Is that even possible? How do I even declare such a thing?

I tried:

<ArrayList>Patients[][] myArray = new ArrayList<Patients>[2][9];

but it didn't work. I'm not really sure how to even make an array[][] of ArrayList-objects.

EDIT

With everyone's help I have now initialized the bidimensional-Arraylist as:

ArrayList<Patients>[][] patientsMatrix = new ArrayList[2][9];

But I'm now kind of stuck at how to enter each element, I tried with this format:

patientsMatrix[0][j].add(myPatientsList.get(i));

I'm getting a java.lang.NullPointerException at the first item it reads, I thought that by declaring the matrix with "new ArrayList[2][9]" at the end it wouldn't throw this kind of exception?

myPatientsList is a patient-type arraylist, could it be what is causing trouble here?

6
  • It is throwing me a compile error: Generic array creation ? Commented Nov 14, 2019 at 16:57
  • 4
    ArrayList<Patients>[][] myArray = new ArrayList[2][9]; Commented Nov 14, 2019 at 17:09
  • 1
    Thanks! That worked Commented Nov 14, 2019 at 17:14
  • "How do I ask and answer homework questions?" is useful to read. It's important to understand that not just students know about Stack Overflow, teachers do too and often you are expected to figure the problem out by yourself, and a teacher seeing your question and matching your answer to the answer given in response, can be embarrassing to you. Commented Nov 14, 2019 at 21:38
  • Possible duplicate of Array of Generic List Commented Nov 14, 2019 at 21:53

2 Answers 2

2

ArrayList<Patients>[][] myArray = new ArrayList<Patients>[2][9];

Sign up to request clarification or add additional context in comments.

2 Comments

Since that exact format threw me a Generic array creation warning I tried with ArrayList<Patients>[][] patientsMatrix = new ArrayList[2][9]; Yet I'm having trouble when trying to add data to the arraylist, it is giving me null pointer exception, Is it because it is missing the <Patients> in the end? Because it wouldn't let me compile unless I changed it :s
No, it's because each cell of the array can hold an ArrayList, but the array is empty when you create it. You need to put a new ArrayList<Patients>() in each cell before you can start adding elements to it.
0

You can also have an ArrayList of ArrayList of ArrayList<Patients>.
Something like ArrayList<ArrayList<ArrayList<Patient>>> patientArray = new ArrayList<>(2)
And then you initialize each of the inner ones like:

for (int i = 0; i < 2; i++) {
  patientArray.add(new ArrayList<ArrayList<Patient>>(9)); 
}

It's essentially a 2-D matrix of dimensions 2x9 of ArrayList<Patients>

9 Comments

That looks a little complicated, so to add an element in this structure would it be with like nested arraylists?
@Mar you basically, keep chaining get until you reach the layer you wish to insert in and then you add the desired value there.
Don't bother. If you know the dimensions of the array will be 2x9 then there's no point using an ArrayList for those two coordinates.
@LowKeyEnergy can you explain why? Why would you miss out on using convenient methods over doing the same by writing a and testing a bunch of code that you could have saved yourself from writing? You know that an ArrayList is essentially an array, right?
Shaka please create a new question. You are going off subject but I'm sure someone will be willing to explain if you don't understand.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.