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?

Mar
  • 93
  • 1
  • 8
  • It is throwing me a compile error: Generic array creation ? – Mar Nov 14 '19 at 16:57
  • 4
    ArrayList[][] myArray = new ArrayList[2][9]; – Allan Braga Nov 14 '19 at 17:09
  • 1
    Thanks! That worked – Mar Nov 14 '19 at 17:14
  • "[How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/128421)" 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. – the Tin Man Nov 14 '19 at 21:38
  • Possible duplicate of [Array of Generic List](https://stackoverflow.com/questions/7810074/array-of-generic-list) – Progman Nov 14 '19 at 21:53
  • About your edit, please ask one question per post. Your original question has been answered, so please mark it as such and, if you need to, ask another question. – Federico klez Culloca Nov 15 '19 at 07:48

2 Answers2

2

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

LowKeyEnergy
  • 638
  • 4
  • 11
  • Since that exact format threw me a Generic array creation warning I tried with ArrayList[][] 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 in the end? Because it wouldn't let me compile unless I changed it :s – Mar Nov 15 '19 at 01:07
  • 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()``` in each cell before you can start adding elements to it. – LowKeyEnergy Nov 15 '19 at 05:00
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>

Shankha057
  • 1,072
  • 14
  • 30
  • That looks a little complicated, so to add an element in this structure would it be with like nested arraylists? – Mar Nov 15 '19 at 00:59
  • @Mar you basically, keep chaining `get` until you reach the layer you wish to insert in and then you `add` the desired value there. – Shankha057 Nov 15 '19 at 01:15
  • 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 Nov 15 '19 at 05:09
  • @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? – Shankha057 Nov 15 '19 at 09:17
  • 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. – LowKeyEnergy Nov 15 '19 at 14:08
  • @LowKeyEnergy I don't need to create a new question. Also, it's not about me understanding, it's about you. Because I'm suggesting a better solution than simply what the OP is asking and broadening the perspective. Please try to understand the situation and come up with comprehensive solutions rather than simply writing direct answers to questions. – Shankha057 Nov 15 '19 at 16:28
  • Also, it doesn't make sense to create arrays of `ArrayList`. – Shankha057 Nov 15 '19 at 16:31
  • That's what he is trying to do. If it doesn't make sense to you, feel free to ask for help. – LowKeyEnergy Nov 15 '19 at 16:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202425/discussion-between-shankha057-and-lowkeyenergy). – Shankha057 Nov 15 '19 at 16:56