计算数组java中的出现次数

时间:2016-11-10 17:01:05

标签: java arrays count indexoutofboundsexception

我想计算Java数组中每个数字的出现次数(例如1 =?,2 =?,3 =?)。我的数组如何存储超过10个值?

String.valueOf(i)

5 个答案:

答案 0 :(得分:1)

public static void main(String[] args){
    int[] arryNum = new int[] { 4, 4, 4, 3, 4, 5, 4, 3, 4, 4, 4, 5, 4, 5, 5, 5, 4, 3, 2, 15, 4,
            3, 4, 6, 4, 3, 4, 5, 4, 2, 4, 5, 4, 3, 2, 5, 4, 3, 5, 4, 0, 4, 3, 4, 5, 4, 3, 0, 4,
            5, 4, 3, 5, 4, 2, 3, 2, 3, 4 };
    Map<Integer, Integer> lookup = new HashMap<>();
    for (int key : arryNum) {
        if(lookup.containsKey(key)) {
            lookup.put(key, lookup.get(key) + 1);
        } else {
            lookup.put(key, 1);
        }
    }

    for (Integer keys : lookup.keySet()) {
        System.out.println(keys + " Found " + lookup.get(keys) + " Times");
    }

}

答案 1 :(得分:1)

你可以这样做。

首先,你需要Map来计算你的东西:

-- Program execution begins here
main :: IO()
main = do
  _ <- getLine
  arr <- map (read :: String -> Integer) . words <$> getLine
  queryList <- readData
  let queries = map (read :: String -> Integer) queryList
  putStrLn ""
  mapM_ print $ compute queries arr  

-- Construct a sublist
sublist :: Integer -> Integer -> [Integer] -> [Integer]
sublist start end list = take (fromInteger end - fromInteger start) . drop (fromInteger start) $ list

-- Calculate the resulting list
compute :: [Integer] -> [Integer] -> [Integer]
compute [_] [_] = []
compute [_] [] = []
compute [_] (_:_) = []
compute [] (_:_) = []
compute [] [] = []
compute (x1:x2:xs) list = result : compute xs list where
  result = frequency $ sublist x1 x2 list 

-- Read query list, end at terminating condition
readData :: IO [String]
readData = do
  x <- getLine
  if x == "0"
    then return []
    else do xs <- readData
            return (words x ++ xs)

-- Return count of the most frequent element in a list
frequency :: [Integer] -> Integer
frequency list = toInteger (snd $ maximumBy (compare `on` snd) counts) where
  counts = nub [(element, count) | element <- list, let count = length (filter (element ==) list)]

然后你迭代你的数字;最好使用for-each:

Map<Integer, Integer> countsByNumbers = new HashMap<>();

一些注意事项:

  1. 请注意,与任何类型的Java Collection类一样,Maps只处理引用类型;因此声明它使用Integer,而不是原始类型 int
  2. 编译器有一些魔力可以将数组中的 int 值转换为封面下的 Integer 对象
  3. 请注意 Map 是一个接口,但是我们必须实例化一个具体的类,在这种情况下我们只需使用 HashMap

答案 2 :(得分:1)

您可以使用java 8流以更简洁的方式编写它:

Map<Integer,Integer> map = new HashMap<Integer,Integer>();
Arrays.stream(arryNum).forEach(x -> map.put(x , map.computeIfAbsent(x, s -> 0) + 1));
System.out.println(map);

答案 3 :(得分:0)

考虑到数组包含String值,将String放入HashMap。 在插入每个字符串之前,检查映射中是否已经存在它的键 如果是 - 取该键的默认值,即 1,并将其增加 1 并再次插入。 如果没有 - 只需添加默认值为 1 的新键 最终地图将给出“字符串”:字符串出现的次数。

public Map<String, Integer> wordCount(String[] strings) {
  Map<String, Integer> map = new HashMap();
  for(String s : strings){
    if(map.containsKey(s)){
      map.put(s, map.get(s)+1);
    }
    else
    map.put(s, 1);
  }
  return map;
}

答案 4 :(得分:0)

包com.report.automation;

导入 java.util.HashMap; 导入 java.util.Map;

公共课频率{ public static void main(String[] args){

    String value[] = {"Mukesh","Mukesh","Sasi","Senthil","Mukesh","Mukesh"};
    String match = "Mukesh";
     int count = 0;
    for (int j = 0; j <= 5; j++) {
    if (match.equals(value[j])) {       
       
        count++;                
        
    }       
    }
    System.out.println(count);
    
}
}