Map<Integer, Integer>map = new HashMap<Integer, Integer>()
Integer[] nums = new Integer[500]// 定义一个长度500的整数组
Random ra = new Random()
// 循环给数组赋值(随机产生值)
for (int i = 0i <nums.lengthi++) {
nums[i] = ra.nextInt(50) + 1// 1-50之间
}
Integer count = 0// 出现的次数
for (int i = 0i <nums.lengthi++) {
if (nums[i] % 2 == 0) {
for (int j = 0j <nums.lengthj++) {
if (nums[i].equals(nums[j])) {
count++
}
if (j == nums.length - 1) {// 最后赋值给map
map.put(nums[i], count)
count = 0
}
}
}
}
System.out.println("排序前:" + map)
List<Map.Entry<Integer, Integer>>mappingList = null
mappingList = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet())
Collections.sort(mappingList,
new Comparator<Map.Entry<Integer, Integer>>() {
public int compare(Map.Entry<Integer, Integer>mapping1,
Map.Entry<Integer, Integer>mapping2) {
// 降序或升序只需要更改参数位置
return mapping2.getValue().compareTo(
mapping1.getValue())
}
})
System.out.print("排序后:")
for (Map.Entry<Integer, Integer>mapping : mappingList) {
System.out.print(mapping.getKey() + ":" + mapping.getValue() + "\t")
}
String[] str = mappingList.get(0).toString().split("=")
System.out.println("\n出现次数最多的偶数:"+str[0]+",出现的次数为:"+str[1])
}
#include "stdio.h"int main()
{
int x[100],max,min,i,n
printf("输入整数个数:")
scanf("%d",&n)
printf("输入一组正整数:")
scanf("%d",&x[0])
max=0min=0
for(i=1i<ni++)
{
scanf("%d",&x[i])
if(x[max]<x[i]) max=i
if(x[min]>x[i]) min=i
}
printf("最大值是%d,下标(从0开始)%d\n最小值是%d,下标(从0开始)%d\n",x[max],max,x[min],min)
}
数组只能是object类型的数组,不能是数组类型的数组。你可以通过定义一个vector或者map类型的数组。
或者自己自定义一个类,class AAA{ private int[] aaa = null} 类中包含一个数组的变量吧。
然后可以定义AAA类型的数组啊, AAA[] a = new AAA[10]