How to store an array of pairs in Java?

前端 未结 9 1355
日久生厌
日久生厌 2020-12-15 06:51

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

        
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 07:35

    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 in Java?

提交回复
热议问题