2

I'm new to Java. I'm wondering what will be the best option to store 2D array with different type of data.

It will be table of countries, each has capital and is in cotinent. Then I have to store it this way: ContinentID | Country name | Capital

What to choose?

1
  • This sounds like a simple List<Country> where Country is an object containing the data for each country. Possibly a Map<String,Country> if you need to search by country name. I'm not sure what you mean by 2D in this context. Can you please clarify? Commented Oct 29, 2012 at 19:05

4 Answers 4

5

You might want to consider making a Country class to hold this data, and then maintaining a list/array of instances of this class.

public class Country {
    private int continentId;
    private String name;
    private String capital;

    public Country(int continentId, String name, String capital) {
        this.continentId = continentId;
        this.name = name;
        this.capital = capital;
    }

    // ...
}

You would then have something along the lines of

List<Country> countries = new ArrayList<Country>();
countries.add(new Country(123, "USA", "Washington DC"));
...
Sign up to request clarification or add additional context in comments.

4 Comments

@Kikert Yes it should work just fine, and will be much easier to manage than having a multi-dimensional array.
But more than just one Country-Capitol, could be in each continent.
@M.M. Yes, id refers to the continent ID.
Nice, I've made it this way. Thanks ;)
0

create a country class with the needed attributes, then create a list, type it as country:

 list<Country> clist = new ArrayList<Country>();

or any list you want. Now just store country objects in the list.

Comments

0

If continent id is just a sequence and does not add any specific meaning, you might want to consider a HashMap with key as country name and value as Capital. If order is important, consider a LinkedHashMap.

If continent id does carry meaning, then you might want to consider moving all the variables to a class, say Country and hold it in a list. If you are planning to retrieve by country name and not iterate, you might want to consider storing the objects in a Hashmap, with key as your country name or capital or whatever suits your need. The reason for a HashMap and not a list is that membership check on a List gives linear performance as against a constant time access on a HashMap.

Comments

0

HashMap<Integer, HashMap<String, String>>();

3 Comments

This would be the complete code: HashMap hm = new HashMap<Integer, HashMap<String, String>>(); ... and this is the complete code with parameterization: HashMap<Integer, HashMap<String, String>> hm = new HashMap<Integer, HashMap<String, String>>();
@hamena314 Um, you can omit the second rodundant parameterization.since Java 7 you can use the diamond <> parameter.
Do you mean HashMap<Integer, HashMap<String, String>> hm = new HashMap<>();? Interesting, did not know that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.