如何存储在Java中对数组? [英] How to store an array of pairs in java?

查看:204
本文介绍了如何存储在Java中对数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java,我想存储对双打的数组。我的code是这样的:

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 ?

推荐答案

创建自己的类来重新present一对,并添加一个构造函数有两个参数:

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

看到这个答案的原因,一个不存在于Java中:<一href=\"http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java\">What是C ++对&LT相当于; L,R&GT;在Java中?

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中对数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆