0

I'm a Comp-Sci Student. I'm working on an assignment in which I have to make a Table class that has a "Key"(Type String) and "Value"(Type Object) property. Sort of like a mini database. Pretty much if I use a get method using "Key" as the parameter it will return the "Value" property.

It came to my mind that I should use a multi-dimensional array of type Object with the first element of a row being the "Key" and the second element being the "Value".

I was wondering if this is bad practice/ or if there is an obvious and more convenient way of going about this.


What I decided to do since hash-maps were dis-allowed: I made 2 separate ArrayLists. One will store Keys the other will store Values. Their indexes will correspond. So the index of a Key passed into a getMethod will return the value of the Value ArrayList at the index of Key since they correspond.

Thanks to everyone who answered. My account is fairly new so I don't have many privileges yet, such as voting. This was a really great preview of how great this community probably is.

5
  • Use a Map<K, T> -- that's exactly what they're for. Commented Nov 15, 2015 at 2:04
  • 2
    Use a Map implementation, such as HashMap Commented Nov 15, 2015 at 2:05
  • Cool! I'll totally look into some tutorials on that. Commented Nov 15, 2015 at 2:06
  • DAMN! It was too good to be true. It seemed to convenient. I read the constraints and it said that I can't use a hash map :^) Commented Nov 15, 2015 at 2:09
  • This sounds like the typical data structures assignment, where you have to implement a map yourself. And yes, you can make pretty much anything out of an array, which is likely to be the expected direction for this assignment. Commented Nov 15, 2015 at 2:33

3 Answers 3

1

There's nothing wrong in principle with using an Object[] as part of the private inner workings of a class if that is the most convenient data type to use.

Many container classes, such as ArrayList and android's SparseArray, do exactly this.

In your case it sounds like a more convenient type to use is HashMap<String, Object> as this allows very fast basic operations.

If you are not allowed to use HashMap it is a good idea to look up how a hash table works and consider implementing such a structure yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your great answer! HashMap was disallowed so I am very much about to look up how they function. Also thanks for the bit about it being okay to use an Object array. It was just another thing that seemed too convenient.
Just don't overuse them. A public method should very rarely return an Object[] for example.
0

You can use generics, similar to how the Map interface does. Since this is for school I imagine your professor wants you to implement it yourself. But here is how a possible solution might look like and an example main method to test it out.

public class Table<K, V> {

    public static void main(String args) {

        Table.Entry<String, Integer> table = new Table.Entry<>();
        table.key = "some key";
        table.value = 10;

    }

    public static class Entry<K, V> {

        K key;
        V value;

    }

}

1 Comment

Thanks for the awesome response! unfortunately I haven't learned about Generics yet. We will be going over it in about a week.
0

The best way to accomplish this would be by creating a separate KeyValue class:

public class KeyValue {
private String key;
private String value;

public KeyValue(String key, String phoneNumber) {
    this.key = key;
    this.value = phoneNumber;
}

public String getKey() {
    return key;
}

public String getValue() {
    return value;
}

public static KeyValue keyvalue(String key, String value) {
    return new KeyValue(key, value);
}

}

In the Main class use the following code:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    private static Scanner scanner=new Scanner(System.in);
    private static ArrayList<KeyValue> newkeyValue=new ArrayList<KeyValue>();


    public static void main(String[] args) {
        addValue();
        printKeyValue();
    }


    // Line 24 uses factory static method to create object newVariable (Instance of KeyValue) 
    public static void addValue(){
        System.out.println("Enter the Key: ");
        String x=scanner.nextLine();
        System.out.println("Enter the Value: ");
        String valOfString=scanner.nextLine();
        KeyValue newVariable=KeyValue.keyvalue(valOfString,x);
        newkeyValue.add(newVariable);
    }



    public static void printKeyValue() {
        System.out.println("Key & Value");
        for(int i=0; i<newkeyValue.size(); i++) {
            System.out.println((i+1) + "." +
                    newkeyValue.get(i).getKey() + " -> " +
                    newkeyValue.get(i).getValue());
        }

    }
}

Comments

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.