Storing number pairs in java

后端 未结 5 734
栀梦
栀梦 2021-01-17 11:54

How do I store a set of paired numbers in java? Do I use lists or arrays or maybe something else?

eg. [ (1,1) , (2,1) , (3,5)]

5条回答
  •  感情败类
    2021-01-17 12:55

    If you can live with low level structures and desperately need compact form of "literal" form of "set of pairs" -- this happens to me in unit test, when I need a set of fixtures -- you can simply use an array of arrays:

    int[][] squares = {
        { 1, 1 },
        { 2, 4 },
        { 3, 9 }
    };
    

    But keep in mind that there is no semantic to such a type -- it all depends on proper use, compiler won't give you a warning if you type squares[0][1] when you really wanted squares[1][0].

提交回复
热议问题