Java (1)寻找一堆数字中只出现一次的数字 (2)统计一堆文件后缀名共有几种类型(即统计一堆数字中出现过那几个数字)
作者:互联网
//*************(1)寻找一堆数字中只出现一次的数字***********
package cn.nrsc.test1;
import java.util.ArrayList;
//设计一个方法,传入一个int类型数组,输出这个数组中只出现一次的数字。
public class OnlyOnce {
public static void main(String[] args) {
int[] a = { 1, 2, 5, 0, 5, 100, 3, 10, 1, 2, 3, 6, 8, 9, 5, 4, 7 };
Array(a);
}
public static void Array(int[] a) {
int index = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (i == j) {
continue;
}
if (a[i] == a[j]) {
index = -1;
break;
}
} // for
if (index != -1) {
System.out.print(a[i] + " ");
}
index = 0;
} // for
}// main
}
//**(2)统计一堆文件后缀名共有几种类型(即统计一堆数字中出现过那几个数字)**
package cn.nrsc.test1;
import java.util.ArrayList;
//import javax.swing.event.ListSelectionEvent;
public class HouZui {
public static void main(String[] args) {
String[] str = new String[7];
str[0] = "test.txt";
str[1] = "demo.java";
str[2] = "test.java";
str[3] = "score.doc";
str[4] = "score.ppt";
str[5] = "score.java";
str[6] = "score.doc";
ArrayList<String> list = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
for (int i = 0; i < str.length; i++) {
list.add(str[i]);
} // for
for (int i = 0; i < list.size(); i++) {
String str1 = list.get(i);
String[] str2 = str1.split("\\.");
list2.add(str2[1]);
// System.out.println(str2[0] + " +++ " + str2[1]);
// System.out.println(str2[1]);
} // for
for (int i = 0; i < list2.size(); i++) {
for (int j = i + 1; j < list2.size(); j++) {
if (list2.get(i).equals(list2.get(j))) {
list2.remove(j);
}
}
} // for
System.out.println("集合中共有" + list2.size() + "种文件后缀");
System.out.println("他们分别为:"+list2);
}
}
标签:一堆,数字,int,ArrayList,list2,后缀名,++,str,String 来源: https://blog.51cto.com/u_15288542/3030147