0

I am a beginner in java programming.

How do you create and initialize an array which contains a single and two dimensional array like below in an Arraylist or array by populating the value of each indexes within a for loop. any help will be much appreciated

{
"john":  
      {  
         "age":"20",
         "eye color":"blue",
         "location":"somewhere in the world",
         "education-level":"degree",
       }

"ben" : 
      {  
         "age":"16",
         "eye color":"green",
         "location":"somewhere in the world",
         "education-level":"secondary school",
       }

}

The idea is to create an array that stores peoples information as seen above

Martin
  • 57
  • 1
  • 7
  • I'm not sure I understand the question. The JSON you show has more to do with maps than arrays or lists. Can you show how you intend to access the data from your multidimensional array? – Jason Bullers Feb 18 '16 at 03:42
  • The idea is to create an array that stores personal information a key pair value. I would like to access it using a for loop e.g `for(int i = 0 ; i < people.length(); i++){ //System.out.println(people.get(i).toString()); for (int j = 0; j < people.get(i).length(); j++{ //System.out.println(people.get(j).toString()); } }` something along those line, if that makes sence – Martin Feb 18 '16 at 04:23
  • 1
    I would create a `Person` object that has each of those fields unless you have some very specific reason why you can't. You can then create the two `Person` instances and put them in a list. – Jason Bullers Feb 18 '16 at 04:27

3 Answers3

1

You can create and initialize a 2D array in Java like this for your data.

String[][] array = new String[][]{
        {"john","20","blue","somewhere in the world","degree"},
        {"ben","16", "green", "somewhere in the world", "secondary school"}         
    };

But for your data it could be better to have some data structure like follows.

Person.java

public class Person{
    private String name;
    private PersonDetails details;
//getters and setters
}

PersonDetails.java

public class PersonDetails{
    private int age;
    private String eyeColor;
    private String location;
    private String educationLevel;
//getters and setters
}

main function

List<Person> personsList = new ArrayList<>();
// loop starts
PersonDetails pd = new PersonDetails();
//set values
Person p = new Person();
//set values
personsList.add(p);
//loop ends
Cozimetzer
  • 662
  • 4
  • 12
0

you can refer to this link:

How to create a Multidimensional ArrayList in Java?

create one multidimensional array list then change the string with T so it will be able to take objects

Community
  • 1
  • 1
MrRizk
  • 111
  • 5
  • 14
0

Your JSON has a bad syntax, the correct way is get a json like this

{
"people" : 
[
    {  "name":"john",
        "age":"20",
        "eye color":"blue",
        "location":"somewhere in the world",
        "education-level":"degree"
    },
    {  
        "name":"ben",
        "age":"16",
        "eye color":"green",
        "location":"somewhere in the world",
        "education-level":"secondary school"
    }
]
}

The [ and ] define an array of elements, and the { and } represents objects of your data type; in this case people.

Then you can get an array of ´people´ object with the necesary fields for information. I have this to serialize and deserialize java objects to json and visceversa, try this. Hope help you.

Rosendo Ropher
  • 496
  • 8
  • 21