Tintinabulator Zea
Tintinabulator Zea

Reputation: 2827

How to have different types in Multi dimensional array Java

Question from my Java class that I don't understand:

Declare a multi-dimensional array of type Person that can be used to store a chore list for a group of people for every day of the week and every week of the year.

How do I declare this? It would be Dimension1 person D2 String D3 int d4 int.

I thought you couldnt have different types so maybe im misunderstanding question? Thanks for any help.

Here's person class:

package ProvidedClasses;

public class Person 
{
private String name;

public Person()
{
    name = "John Doe";
}

public String getName()
{
    return name; 
}

}

Upvotes: 1

Views: 293

Answers (2)

Alex
Alex

Reputation: 171

The beauty of Java is that (almost) everything is an Object. So you may have multidimensional array of Objects and just remember that first index is Person, second is String and third is Integer.

However arrays are lower level and they are rarely used for dynamic data storing. Use Collections and their advantages. It's not clear what you want to achieve but here is one possible solution:

HashMap<String,HashMap<String,HashMap<String,Person>>> chore;
//So, access could be like 
chore.get("Friday").get("25").get("Andy"); // get Andy on Friday of 25-th week.

Or

HashMap<String,HashMap<String,ArrayList<Person>>> choreN;
chore.get("Friday").get("25").get(12); // 12-th person on Friday 25-th week.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201537

A multidimensional array of type person (unless I'm missing something), and given that you will have 7 days in any given week (and there are 52 weeks in a year) and assuming you have x people (10 for the sake of argument) in your group that might look something like

int x = 10;
Person[][][] multidimensionalArrayOfPersons = new Person[x][7][52];

Then, your Person class should have some Chore(s).

Upvotes: 0

Related Questions