如何在java中存储一对数组?

时间:2022-01-18 15:40:29

I am new to java, I want to store an array of pair of doubles. My code looks like this:

我是java新手,我想存储一对双精度浮点数的数组。我的代码是这样的:

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 ?

如何初始化对变量?是否有更好的结构来存储一对双精度浮点数?

8 个解决方案

#1


15  

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?

为什么Java中不存在对:在Java中与c++对 等价的是什么? ,r>

#2


5  

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.

你不想使用它是一个接口,而不是一个类。当您在实现Map的类上调用entrySet()时,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.

你想做(但不能做)的是这个。如果您尝试这样做,您会看到一个编译错误,沿着“不能实例化类型Map.Entry”的代码行。这是因为地图。入口是接口,不是类。接口不包含任何真正的代码,所以这里没有真正的构造函数来运行。

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

如果你看看下面的文档,你可以清楚地看到顶部是一个“接口映射”。这意味着它是一个接口。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.

你应该做的不是实例化接口,这是不可能的,而是创建你自己的类Pair。是这样的。如果使用下面的代码,请记住更改包。

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.

在编写了您的Pair类之后,您的代码现在看起来是这样的。

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);
    }

}

#3


2  

Couldn't you just use

你不能使用

public class MyClass<A,B> extends ArrayList{
private A first;
private B second;
public MyClass(A first, B second){
super();
this.first = first;
this.second = second;}
}

and then add some form of add method, along with a first and second accessor & mutator method? I'm sort of new to programming, but this way would seem like it might work, and be accessible to things other than just the DOUBLE, (in case down the road you want to use other types, like Integer, or even String).

然后添加一些add method的形式,以及第一和第二accessor & mutator方法?我对编程有点陌生,但这种方式似乎可以工作,而且除了双精度函数之外,其他东西也可以访问它(以防将来您希望使用其他类型,比如Integer,甚至String)。

#4


1  

You could use a map to solve this.

你可以用地图来解决这个问题。

#5


1  

Is Entry a class you defined? You instantiate it with new.

输入是您定义的类吗?你用new实例化它。

Entry<Double, Double> pair = new Entry<Double, Double>(d1, d2);

Note I am assuming you defined a constructor that takes 2 doubles, and you have references for d1 and d2.

注意,我假设你定义了一个具有2倍的构造函数,你有d1和d2的引用。

I suggest you NOT use the Map.Entry class. The semantics for that class are such that the values are a key and a value, fitting with the way Maps work.

我建议你不要使用地图。Entry类。该类的语义是这样的:值是键和值,与映射的工作方式相匹配。

#6


0  

If you have access to Entry class, you could create a constructor that takes key and value as parameters.

如果您可以访问Entry类,您可以创建一个构造函数,该构造函数将键和值作为参数。

Entry<Double, Double> pair = new Entry<Double, Double>(0.5, 3.6);
values.add(pair);

#7


0  

The Map.Entry type that you are trying to use is just an interface, and can therefore not be instantiated. If you wanted to (mis)use internal types of Map, then the concrete Map.Entry implementation HashEntry would be an option.

地图。您正在尝试使用的条目类型只是一个接口,因此不能实例化。如果您想(mis)使用内部类型的映射,则使用具体的映射。条目实现HashEntry是一个选项。

It is however a much better idea to implement you own Pair type. Or to use a Map instead of an array if that suits you needs.

不过,实现您自己的Pair类型是一个更好的主意。或者使用映射而不是数组,如果你需要的话。

#8


0  

Another approach, and probably the most efficient way to store and array of double pairs is to use a single array of doubles, and use (2*i) and (2*i + 1) as your indexing scheme. Additionally you gain the advantage that the array will be initialized to all 0s when you create it, no additional steps required. Unfortunately there is a little extra coding overhead to implement add() and remove(), but surprisingly, it's probably less than creating your own container class for the pair.

另一种可能是存储和数组双对的最有效的方法是使用一个双对数组,并使用(2*i)和(2*i + 1)作为索引方案。此外,您还可以获得这样的优势:在创建数组时,数组将被初始化为所有的0,不需要额外的步骤。不幸的是,实现add()和remove()会有一些额外的编码开销,但令人惊讶的是,这可能比为它们创建自己的容器类要少。

class MyClass {
    double[] values;
    int count;

    MyClass(int initialCapacity) {
        values = new double[initialCapacity*2];
    }

    // adding a pair
    void addPair(double x, double y) {
        if (count*2 >= values.length) {
            values = Arrays.copyOf(values, values.length*2);
        }
        values[count*2] = x;
        values[count*2 + 1] = y;
        count++;
    }

    void remove(int index) {
        if (index >= count) throw new IndexOutOfBoundsException();

        if (index < --count) {
            System.arraycopy(values, (index+1)*2, values, index*2, (count - index) * 2);
        }
    }

    int size() { return count; }

    // both these should check that index < count.
    double getX(int index) { return values[index*2]; }
    double getY(int index) { return values[index*2 + 1]; }

    void exampleIteration() {
        // getX/Y accessors are examples of how to get
        // the values, but it will be more efficient
        // in most cases to just access the array
        // array directly as so...
        for (int i=0 ; i<count ; ++i) {
            System.out.printf("%d: (%f,%f)%n", i, values[i*2], values[i*2+1]);
        }
    }
}

#1


15  

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?

为什么Java中不存在对:在Java中与c++对 等价的是什么? ,r>

#2


5  

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.

你不想使用它是一个接口,而不是一个类。当您在实现Map的类上调用entrySet()时,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.

你想做(但不能做)的是这个。如果您尝试这样做,您会看到一个编译错误,沿着“不能实例化类型Map.Entry”的代码行。这是因为地图。入口是接口,不是类。接口不包含任何真正的代码,所以这里没有真正的构造函数来运行。

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

如果你看看下面的文档,你可以清楚地看到顶部是一个“接口映射”。这意味着它是一个接口。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.

你应该做的不是实例化接口,这是不可能的,而是创建你自己的类Pair。是这样的。如果使用下面的代码,请记住更改包。

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.

在编写了您的Pair类之后,您的代码现在看起来是这样的。

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);
    }

}

#3


2  

Couldn't you just use

你不能使用

public class MyClass<A,B> extends ArrayList{
private A first;
private B second;
public MyClass(A first, B second){
super();
this.first = first;
this.second = second;}
}

and then add some form of add method, along with a first and second accessor & mutator method? I'm sort of new to programming, but this way would seem like it might work, and be accessible to things other than just the DOUBLE, (in case down the road you want to use other types, like Integer, or even String).

然后添加一些add method的形式,以及第一和第二accessor & mutator方法?我对编程有点陌生,但这种方式似乎可以工作,而且除了双精度函数之外,其他东西也可以访问它(以防将来您希望使用其他类型,比如Integer,甚至String)。

#4


1  

You could use a map to solve this.

你可以用地图来解决这个问题。

#5


1  

Is Entry a class you defined? You instantiate it with new.

输入是您定义的类吗?你用new实例化它。

Entry<Double, Double> pair = new Entry<Double, Double>(d1, d2);

Note I am assuming you defined a constructor that takes 2 doubles, and you have references for d1 and d2.

注意,我假设你定义了一个具有2倍的构造函数,你有d1和d2的引用。

I suggest you NOT use the Map.Entry class. The semantics for that class are such that the values are a key and a value, fitting with the way Maps work.

我建议你不要使用地图。Entry类。该类的语义是这样的:值是键和值,与映射的工作方式相匹配。

#6


0  

If you have access to Entry class, you could create a constructor that takes key and value as parameters.

如果您可以访问Entry类,您可以创建一个构造函数,该构造函数将键和值作为参数。

Entry<Double, Double> pair = new Entry<Double, Double>(0.5, 3.6);
values.add(pair);

#7


0  

The Map.Entry type that you are trying to use is just an interface, and can therefore not be instantiated. If you wanted to (mis)use internal types of Map, then the concrete Map.Entry implementation HashEntry would be an option.

地图。您正在尝试使用的条目类型只是一个接口,因此不能实例化。如果您想(mis)使用内部类型的映射,则使用具体的映射。条目实现HashEntry是一个选项。

It is however a much better idea to implement you own Pair type. Or to use a Map instead of an array if that suits you needs.

不过,实现您自己的Pair类型是一个更好的主意。或者使用映射而不是数组,如果你需要的话。

#8


0  

Another approach, and probably the most efficient way to store and array of double pairs is to use a single array of doubles, and use (2*i) and (2*i + 1) as your indexing scheme. Additionally you gain the advantage that the array will be initialized to all 0s when you create it, no additional steps required. Unfortunately there is a little extra coding overhead to implement add() and remove(), but surprisingly, it's probably less than creating your own container class for the pair.

另一种可能是存储和数组双对的最有效的方法是使用一个双对数组,并使用(2*i)和(2*i + 1)作为索引方案。此外,您还可以获得这样的优势:在创建数组时,数组将被初始化为所有的0,不需要额外的步骤。不幸的是,实现add()和remove()会有一些额外的编码开销,但令人惊讶的是,这可能比为它们创建自己的容器类要少。

class MyClass {
    double[] values;
    int count;

    MyClass(int initialCapacity) {
        values = new double[initialCapacity*2];
    }

    // adding a pair
    void addPair(double x, double y) {
        if (count*2 >= values.length) {
            values = Arrays.copyOf(values, values.length*2);
        }
        values[count*2] = x;
        values[count*2 + 1] = y;
        count++;
    }

    void remove(int index) {
        if (index >= count) throw new IndexOutOfBoundsException();

        if (index < --count) {
            System.arraycopy(values, (index+1)*2, values, index*2, (count - index) * 2);
        }
    }

    int size() { return count; }

    // both these should check that index < count.
    double getX(int index) { return values[index*2]; }
    double getY(int index) { return values[index*2 + 1]; }

    void exampleIteration() {
        // getX/Y accessors are examples of how to get
        // the values, but it will be more efficient
        // in most cases to just access the array
        // array directly as so...
        for (int i=0 ; i<count ; ++i) {
            System.out.printf("%d: (%f,%f)%n", i, values[i*2], values[i*2+1]);
        }
    }
}