0

I have Kvote class like this:

public class Kvote {
    private static int counter = 0;
    private double[] vrednost = new double[3];
    private int id; 

    public Kvote(double v1, double v2) {
        super();
        this.vrednost[1] = v1;
        this.vrednost[2] = v2;
        this.id = counter;
        counter++;

    }

    public static int getCounter(){
        return counter;
    }
}

I have created several objects of that class in Main:

Kvote k1 = new Kvote(2, 4.25);
Kvote k2 = new Kvote(1.85, 5);
Kvote k3 = new Kvote(1.62, 6.5);
Kvote k4 = new Kvote(1.72, 6);

So, every instance of class Kvote has some id, it's 0 for k1, 1 for k2, 2 for k3... Is it possible to access objects by it's id var value. For example if I ask for object with Id=2, I get object k3...

It's hard for me to explain why I need this, but I do. Basically, my alghorytm needs to work with infinite number of objects of class Kvote: k1,k2,k3,k4,k5,k6,k7....

1
  • 4
    try to add the item to a map using id as a key, please see my answer below Commented Jan 15, 2015 at 18:37

4 Answers 4

1

Try this:

HashMap<Integer, Kvote> map = new HashMap<Integer, Kvote>();
put(k1.getId(), k1);
put(k2.getId(), k2);
put(k3.getId(), k3);
put(k4.getId(), k4);

Then use the map to get values by id like:

map.get(2) => k3

also, you will need to add getId() function to Kvote class.

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

Comments

1

If you are just giving them IDs in sequential order, then you can just use an ArrayList.

List<Kvote> myList = new ArrayList<Kvote>();
myList.add(new Kvote(2, 4.25));
myList.add(new Kvote(1.85, 5));
myList.add(new Kvote(1.62, 6.5));
myList.add(new Kvote(1.72, 6));

And to access the objects, just do this:

myList.get(3); // returns the 4th item in the list

However, if you want to have more flexibility about what your IDs are, then you can use a map, as @RoeyGolzarpoor suggests in his answer.

Comments

0

A java.util.Map is one of the most common data structures that supports key value pairs. There are multiple implementations available where one of them is the java.util.HashMap.

It works like this:

Map<Integer, Kvote> kvotes = new HashMap<>();

// Save it in the Map
Kvote kvote = new Kvote(123D, 213D);
kvotes.put(kvotes.getId(), kvote);

// Retrieve the kvote
Kvote kvote = kvotes.get(123);

The HashMap offers fast access to you elements and you can also iterate the available keys, values or entries.

// Print the kvotes
kvotes.values().stream().forEach(System.out.println(kvote.toString()));

I you wish to keep the order in which you added your elements (for algorithmical purposes) I suggest that you use a LinkedHashMap which will be able to remember the order for you. The HashMap does not hold the order.

Comments

0

I'm not sure what you want as result, thought Roey's map solution is good and I'd just add that instead of having the counter inside the class, have a counter outside and pass its value to the constructor to set the value of id inside instantiation:

public class Kvote {

private double[] vrednost = new double[2];
private int id; 

public Kvote(double v1, double v2, int id) {
    this.vrednost[0] = v1;
    this.vrednost[1] = v2;
    this.id = id;
}

public static int getId(){
    return this.id;
}
}

In the main class:

int counter = 0;

Kvote k1 = new Kvote(2, 4.25, counter++);
Kvote k2 = new Kvote(1.85, 5, counter++);
Kvote k3 = new Kvote(1.62, 6.5, counter++);
Kvote k4 = new Kvote(1.72, 6, counter++);

Then put them in the map.

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.