方式如下:
public class Test {public static void main(String[] args) {
// 定义字符串
String str = "123123";
// 将字符串转换为字符数组
char[] charArray = strtoCharArray();
// 1出现的次数
int count = 0;
// 遍历数组,找到1出现的次数
for (int i = 0; i < charArraylength; i++) {
// 数组中遍历出来的字符转换成数字,与1匹配
if (IntegerparseInt((charArray[i] + "")) == 1) {
count++;
}
}
Systemoutprintln("1出现:" + count + "次");
}
}
运行结果;
package comdytest;import javaioIOException;
import javautilArrayList;
import javautilCollections;
import javautilList;
import javautilMap;
import javautilRandom;
import javautilSet;
import javautilTreeMap;
import javautilTreeSet;
public class Test {
public static void main(String[] args) throws IOException {
Student[] stu = new Student[10];
stu[0] = new Student("20170101", "小明", new Random()nextInt(41) + 60);
stu[1] = new Student("20170102", "小红", new Random()nextInt(41) + 60);
stu[2] = new Student("20170103", "小华", new Random()nextInt(41) + 60);
stu[3] = new Student("20170104", "小风", new Random()nextInt(41) + 60);
stu[4] = new Student("20170105", "小黄", new Random()nextInt(41) + 60);
stu[5] = new Student("20170106", "小绿", new Random()nextInt(41) + 60);
stu[6] = new Student("20170107", "小帅", new Random()nextInt(41) + 60);
stu[7] = new Student("20170108", "小黑", new Random()nextInt(41) + 60);
stu[8] = new Student("20170109", "小军", new Random()nextInt(41) + 60);
stu[9] = new Student("20170110", "小胖", new Random()nextInt(41) + 60);
Systemoutprintln("排序前:");
for (int i = 0; i < stulength; i++) {
Systemoutprintln(stu[i]);
}
// List集合
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < stulength; i++) {
stuListadd(stu[i]);
}
Systemoutprintln("List排序:");
Collectionssort(stuList); // 排序方法
for (int i = 0; i < stuListsize(); i++) {
Systemoutprintln(stuListget(i));
}
// Set集合
Set<Student> stuSet = new TreeSet<Student>(); // TreeSet自动排序
for (int i = 0; i < stulength; i++) {
stuSetadd(stu[i]);
}
Systemoutprintln("Set排序:");
for (int i = 0; i < stuSetsize(); i++) {
Systemoutprintln(stuSettoArray()[i]);
}
// Map
Map<Student, Object> stuMap = new TreeMap<Student, Object>(); // TreeMap自动排序
for (int i = 0; i < stulength; i++) {
stuMapput(stu[i], i);
}
Systemoutprintln("Map排序:");
Object[] keys = stuMapkeySet()toArray();
for (int i = 0; i < keyslength; i++) {
Systemoutprintln(keys[i]);
}
}
}
class Student implements Comparable<Student> {
private String sid;
private String sname;
private int score;
public Student(String sid, String sname, int score) {
super();
thissid = sid;
thissname = sname;
thisscore = score;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
thissid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
thissname = sname;
}
public int getScore() {
return score;
}
public void setScore(int score) {
thisscore = score;
}
@Override
public int compareTo(Student o) {
// return thisscore - ogetScore(); // 从低到高
int r = ogetScore() - thisscore; // 从高到低
return r == 0 ogetSid()hashCode() - sidhashCode() : r;
}
@Override
public String toString() {
return sname + "(" + sid + ") " + score + "分";
}
}
运行效果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)