Storing a string and two doubles java

后端 未结 3 1795
野的像风
野的像风 2021-01-29 09:55

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)

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 10:15

    A nice generic solution:

    public class Triple {
    
        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 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.

提交回复
热议问题