Too many 'if' statements?

前端 未结 26 1188
天涯浪人
天涯浪人 2020-11-30 16:24

The following code does work how I need it to, but it\'s ugly, excessive or a number of other things. I\'ve looked at formulas and attempted to write a few solutions, but I

26条回答
  •  囚心锁ツ
    2020-11-30 17:05

    I'd use a Map, either a HashMap or a TreeMap

    Especially if the parameters are not on the form 0 <= X < N

    Like a set of random positive integers ..

    Code

    public class MyMap
    {
        private TreeMap map;
    
        public MyMap ()
        {
            map = new TreeMap ();
        }
    
        public void put (int key1, int key2, Integer value)
        {
            String key = (key1+":"+key2);
    
            map.put(key, new Integer(value));
        }
    
        public Integer get (int key1, int key2)
        {
            String key = (key1+":"+key2);
    
            return map.get(key);
        }
    }
    

提交回复
热议问题