简体   繁体   English

存储一个字符串和两个双打java

[英]Storing a string and two doubles java

I have written a program which gives me three arrays.A string array and two dole arrays.... But I want to save them in one thing(I don't know if it would be an array or matrix)..... 我写了一个程序,给我三个数组。一个字符串数组和两个dole数组....但是我想将它们保存在一件事中(我不知道它是数组还是矩阵)。 。

Forexample: I have a file to read and the input is something like: 例如:我有一个文件要读取,输入内容如下:

Apple  2.3  4.5
Orange 3.0  2.1
Pear   2.9  9.6
etc......

I already made three arrays, one that stores the name of strings and the other two stores the two columns of the doubles... 我已经做了三个数组,一个数组存储字符串的名称,另外两个数组存储双打的两列...

But I want to store the whole row "(apple 2.3 4.5)" in one thing so that if I want to find apple I also get related values of apple..... Can you please guys give me a hint how can I do that? 但是我想将整行“(apple 2.3 4.5)”存储在一件事中,这样,如果我想找到苹果,我也会得到apple的相关值.....能否请大家给我一个提示,我该怎么做那? I thought of having three diminutional array but I could not figure out how to initialise because it will have one string value and two doubles...... 我想到了三个减数数组,但是我不知道如何初始化,因为它将有一个字符串值和两个双精度数……

I have no clue what to do.... Any help would be highly appreciated.... Thanks in advance. 我不知道该怎么办。。任何帮助将不胜感激。

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

Then you can do 那你可以做

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

I really suggest you to read a good tutorial on Object Oriented Programming, like my favorite , especially Chapter 25+. 我真的建议您阅读一个有关面向对象编程的好教程,例如我最喜欢的教程,尤其是第25章。

A nice generic solution: 一个不错的通用解决方案:

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

}

Which can be implemented as such: 可以这样实现:

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

But the real concept here is representing a point of data as an object in your code. 但是这里的真正概念是将数据点表示为代码中的对象 If you have a set of data ("I have a string and two ints that mean something "), then you would want to encapsulate it under a single class. 如果您有一组数据(“我有一个字符串和两个表示某些含义的整数”),那么您希望将其封装在一个类下。

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

}

In my example, I use a map corresponding to a list of numbers (doubles). 在我的示例中,我使用对应于数字列表(双精度)的映射。 The key to the map (string) is the string from the first array, and the list contains the numbers corresponding to the string from each other array. 映射的键(字符串)是第一个数组中的字符串,并且列表包含与其他数组中的字符串对应的数字。 The above example gave me the output: 上面的例子给了我输出:

Pear : 2.2 22.22 
Lemon : 3.3 33.33 
Apple : 1.1 11.11 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM