-3

I would like to do some sort of triple array in java to save/load some data, but I want to have some sort of key. Let's say I want a list, that contains the phone numbers of 2 of my friends, William and Mike. So I want to do something like getPhoneNumber("William"); and it will return the number based on the string "William" I gave it. I am not sure how to explain this, I hope someone can guide me.

EDIT: I forgot to mention, I need to save 3 things. So let's say based on the name "William" It should have like, phone number and age or something like that

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • I think you should try to explain it more clearly; in this way you will end up with a better analysis of the problem too. – watery Feb 16 '14 at 13:49
  • HashMap. You can keep your three objects in an array or a custom object, inserted in the map. – Hot Licks Feb 16 '14 at 13:53
  • Sorry I messed up a bit the whole thing, I wasn't sure how to explain it. about HashMap, could you give me an example how to store 3 objects? so I want the first to be used as a key, and return the other 2 objects – user1561084 Feb 16 '14 at 13:55
  • Create an object to store the other two. Put that object into the Map as the "value". – Hot Licks Feb 16 '14 at 20:46

1 Answers1

2

Try using the classes that implement the Map interface, meaning that they provide the functionalities you are looking for.

HashMap is one of such classes:

import java.util.HashMap;

class ContactInformation {
    public String phone;
    public int age;
}

public class Main {
    public static void main(String[] args) {
        HashMap<String, ContactInformation> phones = new HashMap<String, ContactInformation>();

        String name = "William";
        ContactInformation ci = new ContactInformation();
        ci.phone = "...";
        ci.age = 21;

        phones.put(name, ci);
        System.out.println(phones.get(name).phone);
        System.out.println(phones.get(name).age);
    }
}

You might also run into Hashtable, but I think that's overkill for what you want. See this question for an explanation of the difference between HashMap and Hashtable.

Community
  • 1
  • 1
Allan
  • 525
  • 2
  • 7
  • 1
    Please provide an example of usage, to make a decent answer. – christopher Feb 16 '14 at 13:47
  • Thanks but I forgot to mention, I need 3 columns, so Map will not work since it supports 2. Is there something similar that accepts 3? – user1561084 Feb 16 '14 at 13:49
  • You can create a class with all the attributes you need. For instance, a `ContactInformation` class with `phone` and `email` attributes, and then have your `HashMap` be like ``. – Allan Feb 16 '14 at 13:55
  • 1
    @Allan I second that; if you need to store more info regarding the same 'reference' (the person name here), it's best to write a separate object that will store all the information and then use a Map like here suggested. So that if in the future another info should be added to the person, you will only change the object properties, but not the map. – watery Feb 16 '14 at 13:57
  • Thanks, I created it a bit differently but I still used hashmap and class similar to your example. Thanks a lot! – user1561084 Feb 16 '14 at 14:39