1 package com.it18day19; 2 /** 3 * 字符串值修改 4 * @author feigu 5 * 6 */ 7 public class SplitString { 8 9 public static void main(String[] args) { 10 String str="session_count=2|1s_3s=1|4s_6s=0|7s_9s=0"; 11 //查找的字符串 12 String str2="1s_3s"; 13 //想要修改为的值 14 int num=3; 15 System.out.println("原来字符串为:"); 16 System.out.println(str); 17 System.out.println("修改后字符串为:"); 18 String s=split(str, str2, num); 19 System.out.println(s); 20 21 } 22 public static String split(String str,String str2,int num){ 23 String endStr=""; 24 //根据"|"切割字符串为数组 25 String[] str1=str.split("\|"); 26 27 //遍历数组,如果数组元素=查找字符串,就对其值进行修改,返回(原来值+设置的值) 28 for(int i=0;i<str1.length;i++){ 29 if(str1[i].substring(0, str1[i].length()-2).equals(str2)){ 30 int sumNum=0; 31 int endNum=0; 35 endNum=Integer.parseInt(str1[i].substring(str1[i].length()-1, str1[i].length())); 37 str1[i]=str1[i].substring(0, str1[i].length()-1)+(num+endNum); 38 } 39 //重新合并数组为字符串 40 //如果不是最后一个元素,就再添加|; 41 if(i!=str1.length-1){ 42 endStr +=str1[i]+"|";//+=是在字符串上继续添加 43 } 44 else{ 45 //如果是最后一个元素,不需添加| 46 endStr +=str1[i]; 47 } 48 } 49 //返回重新组合的字符串 50 return endStr; 51 } 52 }
1 package com.it18day19; 2 3 import java.util.ArrayList; 4 5 /** 6 * 把列表中的TOP3的最大数放在数组中 7 * 8 * @author feigu 9 * 10 */ 11 public class PutSort { 12 13 public static void main(String[] args) { 14 // TODO Auto-generated method stub 15 ArrayList<Integer> list = new ArrayList<Integer>(); 16 list.add(87); 17 list.add(69); 18 list.add(37); 19 list.add(97); 20 list.add(19); 21 list.add(80); 22 list.add(46); 23 int max = 0; 24 int seMax = 0; 25 int thMax = 0; 26 //定义存放前三个最大数的容器数组 27 int[] arr = new int[3]; 28 //取出最大值 29 for (Integer inte : list) { 30 if (max < inte) { 31 max = inte; 32 } 33 } 34 //取出第二最大值 35 for (Integer inte : list) { 36 if (inte == max) {//如果碰到最大值,跳出本次循环 37 continue; 38 } else if (seMax < inte) { 39 seMax = inte; 40 } 41 } 42 //取出第三最大值 43 for (Integer inte : list) { 44 if (inte == max || inte == seMax) {//碰到前2大数值,跳出本次循环 45 continue; 46 } else if (thMax < inte) { 47 thMax = inte; 48 } 49 } 50 System.out.println("集合中元素排序为:"); 51 for(Integer i:list){ 52 System.out.print(i+" "); 53 } 54 System.out.println(); 55 arr[0] = max; 56 arr[1] = seMax; 57 arr[2] = thMax; 58 System.out.println("TOP前3的数分别为:"); 59 for (int i : arr) { 60 System.out.print(i+" "); 61 } 62 } 63 } 64 第二种方法 65 package com.it18zhangday20; 66 /** 67 * 把列表中的top3的数值搞到数值中,(不允许对改变列表) 68 */ 69 import java.util.ArrayList; 70 import java.util.Iterator; 71 import java.util.List; 72 73 public class Trans { 74 75 public static void main(String[] args) { 76 // TODO Auto-generated method stub 77 Integer[] top3=new Integer[3]; 78 List<Integer> a = new ArrayList<>(); 79 a.add(12); 80 a.add(15); 81 a.add(16); 82 a.add(52); 83 a.add(56); 84 Iterator<Integer> it = a.iterator(); 85 while(it.hasNext()){ 86 Integer score=it.next(); 87 for(int i=0;i<3;i++){ 88 if(top3[i]==null){ 89 top3[i]=score; 90 break; 91 } 92 else if(score>top3[i]){ 93 for(int j=2;j>i;j--){ 94 top3[j]=top3[j-1]; 95 } 96 top3[i]=score; 97 break; 98 } 99 } 100 } 101 System.out.println(top3[0]); 102 System.out.println(top3[1]); 103 System.out.println(top3[2]); 104 } 105 }