编写一个程序,将数字从键盘读入int[ ]类型的数组中。你可以假设数组中最多有50个条目。你的程序最多允许输入50个数字。输出为一个两列的列表。第一列列出了不同的数组元素;第二列是每个元素的出现次数。这个列表应该按照第一列条目从大到小的顺序排序。对于数组
(For the array)
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
输出应该如下所示:
(the output should be)
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
(For the array)
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
输出应该如下所示:
(the output should be)
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
int values[] = {-12, 3, -12, 4, 1, 1, -12, 1, -1, 1, 2, 3, 4, 2, 3, -12};
Map<Integer, Integer> map = new TreeMap<Integer, Integer>(new Comparator<Integer>(){
@Override
public int compare(Integer val1, Integer val2) {
return val2.compareTo(val1);
}
});;
for(int i = 0; i < values.length; i++){
int value = 1;
if(map.keySet().contains(values[i])){
value = map.get(values[i]) + 1;
}
map.put(values[i], value);
}
for(Map.Entry<Integer, Integer> mm : map.entrySet()){
System.out.println("K: "+mm.getKey()+",V: "+mm.getValue());
}
这样是可以的
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;public class des {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入整型数组,以空格分开 如 :10 5 7 8 9 5 6 5 9");
String[] numberArray=in.nextLine().split(" ");
Integer[] array = new Integer[numberArray.length];
for (int j = 0; j < numberArray.length; j++) {
try{
array[j] = Integer.parseInt(numberArray[j]);
}catch(Exception e){
System.out.println("你输入了非整数, 程序退出!");
return;
}
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
Integer integer = map.get(array[i]);
map.put(array[i], integer == null ? 1 : integer + 1);
}
Object[] key_arr = map.keySet().toArray();
Integer[] keyArray = new Integer[key_arr.length];
for (int j = 0; j < key_arr.length; j++) {
keyArray[j] = (int) key_arr[j];
}
Comparator<Integer> c = new Mycomparator();
Arrays.sort(keyArray, c);
for (Integer key : keyArray) {
Object value = map.get(key);
System.out.println(key + "===" + value);
}
}
}class Mycomparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 > o2)
return -1;
if (o1 < o2)
return 1;
return 0;
}
}
public static void main(String[] args) {
Integer [] a = {-12, 3, -12, 4, 1 ,1 ,-12, 1, -1, 1 ,2, 3, 4 ,2 ,3 ,-13};
Comparator<Integer> cmp = new MyComparator();
Arrays.sort(a, cmp);
int i = 0;
int b = 1;
while (i<a.length-1) {
if (a[i]==a[i+1]) {
b++;
i++;
continue;
}else {
System.out.println(a[i]+" "+b);
b=1;
i++;
}
}
System.out.println(a[i]+" "+b);
}
}}
class MyComparator implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
//如果o1小于o2,我们就返回正值,如果o1大于o2我们就返回负值,
//这样颠倒一下,就可以实现反向排序了
if(o1 < o2) {
return 1;
}else if(o1 > o2) {
return -1;
}else {
return 0;
}
}
}
System.out.println("请输入10个数字!");
Scanner sc = new Scanner(System.in);
Integer[] a = new Integer[10];
for (int m = 0; m < 10; m++) {
int d = sc.nextInt();
a[m] = d;
}
@SuppressWarnings("unchecked")
// 将数组转换成集合
List<Integer> list = Arrays.asList(a);
@SuppressWarnings("unchecked")
//对数组去重、排序
Set<Integer> set = new TreeSet(list);
for (Iterator ite = set.iterator(); ite.hasNext();) {
Integer i = (Integer) ite.next();
//从数组中获取该元素出现的次数
int j = Collections.frequency(list, i);
System.out.println("["+i+","+j+"]");
}
}
Map<Integer, Integer> map = new TreeMap<Integer, Integer>(new Comparator<Integer>(){
@Override
public int compare(Integer val1, Integer val2) {
return val2.compareTo(val1);
}
});;
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
function addPage(){
window.location.href="${ pageContext.request.contextPath}/addproduct.jsp";
}
function delAll(){
document.getElementById("form1").submit();
}
$(function(){
$("#selectAll").click(function(){
$("input[id='ids']").prop("checked",this.checked);
});
});
</script>
</head><body>
<form id="form1"action="${pageContext.request.contextPath }/deleteAllServlet" method="post">
<table width="100%" border="1px">
<tr>
<td colspan=8>
名称<input type="text" name="pname"><input type="submit" value="查询">
<input type="button" value="添加" onclick="addPage()"/>
<input type="button" value="删除" onclick="delAll()"/>
</td>
</tr>
<tr>
<td>序号</td>
<td><input type="checkbox" id="selectAll" onclick="Alldelete()"/></td>
<td>商品名称</td>
<td>出厂价</td>
<td>出售价</td>
<td>是否热门</td>
<td>是否下架</td>
<td>操作</td>
</tr>
<c:forEach items="${list}" var="li" varStatus="s">
<tr>
<td>${s.count}</td>
<td><input type="checkbox" id="ids" name="ids" value="${li.pid}"/></td>
<td>${li.pname}</td>
<td>${li.et_price}</td>
<td>${li.shop_price}</td>
<td>
<c:if test="${li.is_hot == 1}">
是
</c:if>
<c:if test="${li.is_hot == 0}">
否
</c:if>
</td>
<td>
<c:if test="${li.is_hot == 1}">
已下架
</c:if>
<c:if test="${li.is_hot == 0}">
未下架
</c:if>
</td>
<td><a href="${pageContext.request.contextPath }/AlterServlet?pid=${li.pid}">修改</a> <a href="${pageContext.request.contextPath }/DeleteServlet?pid=${li.pid}">删除</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
set1=set(list1)
list2=list(set1)
list2.sort(reverse=True)dic1={}
for i in list2:
n=list1.count(i)
#print(n)
dic1[i]=n
for i in list2:
print('{:^5}{:^5}'.format(str(i),str(dic1[i])))
'''Results:
4 2
3 3
2 2
1 4
-1 1
-12 4
'''
Integer [] a = {-12, 3, -12, 4, 1 ,1 ,-12, 1, -1, 1 ,2, 3, 4 ,2 ,3 ,-13};
Comparator<Integer> cmp = new MyComparator();
Arrays.sort(a, cmp);
int i = 0;
int b = 1;
while (i<a.length-1) {
if (a[i]==a[i+1]) {
b++;
i++;
continue;
}else {
System.out.println(a[i]+" "+b);
b=1;
i++;
}
}
System.out.println(a[i]+" "+b);
}
}}
class MyComparator implements Comparator<Integer>{
@Override
public int compare(Integer a1, Integer a2) {
if(a1 < a2) {
return 1;
}else if(a1 > a2) {
return -1;
}else {
return 0;
}
}