Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
存储一个字符串和两个双精度java_Java_Arrays - Fatal编程技术网

存储一个字符串和两个双精度java

存储一个字符串和两个双精度java,java,arrays,Java,Arrays,我写了一个程序,它给了我三个数组。一个字符串数组和两个dole数组。。。。 但我想用一件事来保存它们我不知道它是数组还是矩阵 例如: 我有一个文件要读,输入如下: Apple 2.3 4.5 Orange 3.0 2.1 Pear 2.9 9.6 etc...... 我已经制作了三个数组,一个存储字符串的名称,另两个存储双精度的两列 但我想把整行苹果2.3 4.5存储在一个东西中,这样如果我想找到苹果,我也可以得到苹果的相关值。。。。。 你们能给我一个提示吗?我该怎么做? 我想用三

我写了一个程序,它给了我三个数组。一个字符串数组和两个dole数组。。。。 但我想用一件事来保存它们我不知道它是数组还是矩阵

例如: 我有一个文件要读,输入如下:

Apple  2.3  4.5
Orange 3.0  2.1
Pear   2.9  9.6
etc......
我已经制作了三个数组,一个存储字符串的名称,另两个存储双精度的两列

但我想把整行苹果2.3 4.5存储在一个东西中,这样如果我想找到苹果,我也可以得到苹果的相关值。。。。。 你们能给我一个提示吗?我该怎么做? 我想用三个小数组,但我不知道如何初始化,因为它将有一个字符串值和两个双精度

我不知道该怎么办。。。。 任何帮助都将不胜感激。。。。 提前谢谢

class Triple {
    private String name;
    private double d1;
    private double d2;

    public Triple(String name, double d1, double d2) {
        this.name = name;
        this.d1 = d1;
        this.d2 = d2;
    }
}
那你就可以了

Triple[] fruits = new Triple[3];
fruits[0] = new Triple("Apple", 42.0, 13.37);

我真的建议您阅读一本关于面向对象编程的好教程,特别是第25章。

一个很好的通用解决方案:

public static void main(String[] args) throws Exception {

    Map<String,List<Double>> theMap = new HashMap<String,List<Double>>();

    String [] fruits = {"Apple","Pear","Lemon"};
    Double [] firstDArray = {1.1,2.2,3.3};
    Double [] secondDArray = {11.11,22.22,33.33};

    for(int i = 0; i < fruits.length; i++){
        List<Double> innerList = new ArrayList<Double>();
        innerList.add(firstDArray[i]);
        innerList.add(secondDArray[i]);
        theMap.put(fruits[i], innerList);
    }

    for(Entry<String,List<Double>> en : theMap.entrySet()){
        System.out.print(en.getKey() + " : ");
        for(Double d : en.getValue()){
            System.out.print(d + " ");
        }
        System.out.println();
    }

}
public class Triple<L, K, V> {

    private final L first;
    private final K second;
    private final V third;

    public Triple(L first, K second, V third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public L getFirst() {
        return this.first;
    }

    public K getSecond() {
        return this.second;
    }

    public V getThird() {
        return this.third;
    }

}
可以这样实施:

Triple<String, Integer, Integer> myTriple = new Triple<>("Hello world", 42, 666);

但这里真正的概念是将数据点表示为代码中的对象。如果您有一组数据,我有一个字符串和两个int,它们表示某种意义,那么您可能希望将其封装在一个类中。

封装。面向对象编程。你已经可以分享你当前的解决方案并解释它的问题了吗?这是一个设计和维护的噩梦。非常感谢Rogue帮我解决这个问题:你为什么用L,K,V作为通用名称?我真的很好奇这背后是否有更深层次的模式,或者只是随机选择,因为我通常使用从T开始的字母表。@Reinder这是一个老习惯,从我创建一个名为TwoKeyMap的类时起,我就想到了锁、键和值。现在回想起来,我可能会把它称为一个具有R行、C列和V值的表。但旧习惯难以改变:
Triple<String, Integer, Integer> myTriple = new Triple<>("Hello world", 42, 666);