I am new to Java, I want to store an array of pair of doubles. My code looks like this:
import java.util.ArrayList;
import java.util.Map.Entry;
List<Entry<Double, Double>> values = new ArrayList<>();
Entry<Double, Double> pair;
// set pair values:
// pair.setKey(0.5); // this method does not exists
// pair.setValue(3.6);
values.add(pair);
How can I initialize the pair variable? Is there a better structure to store my array of pair of doubles?
Approach: Store the pairs in an array using a user-defined Pair class. Override the comparator method to sort the array according to the first element. Sort the array according to the first element.
In order to find all the possible pairs from the array, we need to traverse the array and select the first element of the pair. Then we need to pair this element with all the elements in the array from index 0 to N-1.
Create your own class to represent a pair and add a constructor that takes two arguments:
public class MyPair
{
private final Double key;
private final Double value;
public MyPair(Double aKey, Double aValue)
{
key = aKey;
value = aValue;
}
public Double key() { return key; }
public Double value() { return value; }
}
See this answer for reasons why a Pair
does not exist in Java: What is the equivalent of the C++ Pair<L,R> in Java?
You don't want to use Entry it's an INTERFACE, not a CLASS. That interface is used by an implementations of Set when you call entrySet() on a class that implements Map. It basically lets you manipulate the implemented Map as though it were a Set.
What you would do (but can't) is this. If you try to do this you'll see a compiler error along the lines of "Cannot instantiate the type Map.Entry". That's because Map.Entry is an interface, not a class. An interface doesn't contain any real code, so there's no real constructor to run here.
Entry<Double, Double> pair = new Entry<Double, Double>();
If you look at the below docs you can clearly see at the top that it's a "Interface Map.Entry" which means it's an interface. http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Map.Entry.html
What you should do instead of trying to instantiate an interface, which is impossible, is create your own class called Pair. Something like this. Remember to change the package if you use the below code.
package org.mike.test;
public class Pair {
private double x = 0.0;
private double y = 0.0;
public Pair(double x, double y)
{
this.x = x;
this.y = y;
}
public Pair()
{
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
After writing your Pair class your code will now look like this.
package org.mike.test;
import java.util.ArrayList;
import org.mike.test.Pair; //You don't need this if the Pair class is in the same package as the class using it
public class tester {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<Pair> values = new ArrayList<Pair>();
Pair pair = new Pair();
// set pair values:
pair.setY(3.6);
pair.setX(3.6);
values.add(pair);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With