Java常规题技术分享

一、数组排序和添加成员

设计类Student和类StudentClass。

(1) 类Student有字符串属性name、double属性grade和int属性age

有带参数的构造方法,可设置三个属性的值

有各个属性的置取方法

(2)类StudentClass有Student数组属性stus存放班级成员,有int属性size存入班级人数。

有createClass()方法: 使用下面三个预置数据的数组,为班级添加成员并设置班级人数。

String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"};

double grades[] = {67,78.5,98,76.5,90};

int ages[] = {17,18,18,19,17};

有sort()方法:实现对班级成员按成绩从大到小排序

有output()方法:实现指定格式的班级成员信息输出

有add()方法:实现为班级新增一个学生

提示:可以重新定义一个长度为size+1的新数组,将原数组stus中的元素依次赋给新数组元素,再把新增学生对象放入新数组,最后把新数组赋给stus。其它部分代码可参考程序21.1-21.2。

要求编程实现类Student和类StudentClass,使给定的Test类能正常运行,并实现指定的输出内容。

public class Test

{

public static void main(String[] args)

{

StudentClass sClass = new StudentClass();

sClass.createClass();

System.out.println("Original Order:");

System.out.println(sClass.output());

sClass.sort();

System.out.println("Sorted Order:");

System.out.println(sClass.output());

sClass.add(new Student("Sharon",18,80));

System.out.println("Original Order after Adding:");

System.out.println(sClass.output());

sClass.sort();

System.out.println("Sorted Order after Adding:");

System.out.println(sClass.output());

}

}

java 复制代码
public class Test {

    public static void main(String[] args) {

        StudentClass3 sClass = new StudentClass3();
        sClass.createClass3();

        System.out.println("Original Order:");
        sClass.output();

        sClass.sort();

        System.out.println("Sorted Order:");
        sClass.output();

        sClass.add(new Student1("Sharon", 18, 80));

        System.out.println("Original Order after Adding:");
        sClass.output();

        sClass.sort();

        System.out.println("Sorted Order after Adding:");
        sClass.output();
    }
}

class NameAndGradeArray {
    public String name;
    public double grade;
}

class Student1 {
    public String name;
    public double grade;
    public int age;


    Student1() {
    }

    Student1(String name, int age, double grade) {
        this.name = name;
        this.grade = grade;
        this.age = age;
    }
}

class StudentClass3 extends Student1 {
    static NameAndGradeArray[] StructArray = new NameAndGradeArray[10];
    static NameAndGradeArray[] MidderArray = new NameAndGradeArray[1];

    private int i = 0;
    private int j = 0;
    public int size;


    StudentClass3() {
    }

    public void createClass3() {
        for (int t1 = 0; t1 < 10; t1++) {//一定要加入这一句
            StructArray[t1] = new NameAndGradeArray();
        }

        String names[] = {"Tom", "Jerry", "Snoopy", "Mary", "Rose"};
        double grades[] = {67, 78.5, 98, 76.5, 90};
        int ages[] = {17, 18, 18, 19, 17};
        int x0 = 3;
        int x1 = names.length;
        this.size = x1;

        while (i != x1) {
            StructArray[i].name = names[i];
            StructArray[i].grade = grades[i];
            i++;
        }
        i = 0;
    }

    public void sort() {

        for (i = 0; i < this.size; i++) {
            for (j = i; j < this.size; j++) {
                if (StructArray[i].grade < StructArray[j].grade) {
                    MidderArray[0] = StructArray[i];
                    StructArray[i] = StructArray[j];
                    StructArray[j] = MidderArray[0];
                }
            }
        }
    }

    public void output() {
        i=0;
        for(;i<this.size;i++){
            System.out.println("Name: "+StructArray[i].name+"\tGrade: "+StructArray[i].grade);
        }
        System.out.println("total: " + this.size + " students");
        System.out.println();
    }

    public void add(Student1 stu1) {
        StructArray[i].name = stu1.name;
        StructArray[i].grade = stu1.grade;
        this.size++;

    }

}

二、List排序编程1

设计类Student和类StudentClass,以及学生比较器类StudentComparator。

(1) 类Student有字符串属性name、double属性grade和int属性age

有带参数的构造方法,可设置三个属性的值

有各个属性的置取方法

(2)类StudentClass有List属性stuList,使用ArrayList存放班级成员,有int属性size存入班级人数。

有createClass()方法: 使用下面三个预置数据的数组,为班级添加成员并设置班级人数。

String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"};

double grades[] = {67,78.5,98,76.5,90};

int ages[] = {17,18,18,19,17};

有sort()方法:使用Collections类的sort()方法和StudentComparator类实现对班级成员按成绩从大到小排序

有output()方法:实现指定格式的班级成员信息输出

(3)类StudentComparator要实现Comparator接口

提示:可参考程序21.7-21.8。

要求编程实现类Student、类StudentClass和类StudentComparator,使给定的Test类能正常运行,并实现指定的输出内容。

public class Test

{

public static void main(String[] args)

{

StudentClass sClass = new StudentClass();

sClass.createClass();

System.out.println("Original Order:");

System.out.println(sClass.output());

sClass.sort();

System.out.println("Sorted Order:");

System.out.println(sClass.output());

}

}

java 复制代码
public class Test {

    public static void main(String[] args) {

        StudentClass3 sClass = new StudentClass3();
        sClass.createClass3();

        System.out.println("Original Order:");
        sClass.output();

        sClass.sort();

        System.out.println("Sorted Order:");
        sClass.output();

//        sClass.add(new Student1("Sharon", 18, 80));
//
//        System.out.println("Original Order after Adding:");
//        sClass.output();
//
//        sClass.sort();
//
//        System.out.println("Sorted Order after Adding:");
//        sClass.output();
    }
}

class NameAndGradeArray {
    public String name;
    public double grade;
    public int age;
}

class Student1 {
    public String name;
    public double grade;
    public int age;


    Student1() {
    }

    Student1(String name, int age, double grade) {
        this.name = name;
        this.grade = grade;
        this.age = age;
    }
}

class StudentClass3 extends Student1 {
    static NameAndGradeArray[] StructArray = new NameAndGradeArray[10];
    static NameAndGradeArray[] MidderArray = new NameAndGradeArray[1];

    private int i = 0;
    private int j = 0;
    public int size;


    StudentClass3() {
    }

    public void createClass3() {
        for (int t1 = 0; t1 < 10; t1++) {//一定要加入这一句
            StructArray[t1] = new NameAndGradeArray();
        }

        String names[] = {"Tom", "Jerry", "Snoopy", "Mary", "Rose"};
        double grades[] = {67, 78.5, 98, 76.5, 90};
        int ages[] = {17, 18, 18, 19, 17};
        int x0 = 3;
        int x1 = names.length;
        this.size = x1;

        while (i != x1) {
            StructArray[i].name = names[i];
            StructArray[i].grade = grades[i];
            StructArray[i].age=ages[i];
            i++;
        }
        i = 0;
    }

//    public void sort() {

//        for (i = 0; i < this.size; i++) {
//            for (j = i+1; j < this.size; j++) {
//                if (StructArray[i].age > StructArray[j].age) {
//                    MidderArray[0] = StructArray[i];
//                    StructArray[i] = StructArray[j];
//                    StructArray[j] = MidderArray[0];
//                }
//            }
//        }
//    }
    public void sort() {

        for (i = 0; i < this.size; i++) {
            for (j = i; j < this.size; j++) {
                if (StructArray[i].grade < StructArray[j].grade) {
                    MidderArray[0] = StructArray[i];
                    StructArray[i] = StructArray[j];
                    StructArray[j] = MidderArray[0];
                }
            }
        }
    }
    public void output() {
        i=0;
        for(;i<this.size;i++){
            System.out.println("Name: "+StructArray[i].name+"\tGrade: "+StructArray[i].grade);//+"\tGrade: "+StructArray[i].grade  "Age: "+StructArray[i].age+
        }
        System.out.println("total: " + this.size + " students");
        System.out.println();
    }

    public void add(Student1 stu1) {
        NameAndGradeArray newStu=new NameAndGradeArray();
        StructArray[i].name = stu1.name;
        StructArray[i].grade = stu1.grade;
        StructArray[i].age=stu1.age;
        StructArray[size]=newStu;
        this.size++;

    }

}

三、List排序编程2

设计类Student和类StudentClass。

(1) 类Student有字符串属性name、double属性grade和int属性age

有带参数的构造方法,可设置三个属性的值

有各个属性的置取方法

要实现Comparable接口

(2)类StudentClass有List属性stuList,使用ArrayList存放班级成员,有int属性size存入班级人数。

有createClass()方法: 使用下面三个预置数据的数组,为班级添加成员并设置班级人数。

String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"};

double grades[] = {67,78.5,98,76.5,90};

int ages[] = {17,18,18,19,17};

有sort()方法:使用Collections类的sort()方法实现对班级成员按成绩从大到小排序

有output()方法:实现指定格式的班级成员信息输出

提示:可参考程序21.10。

要求编程实现类Student和类StudentClass,使给定的Test类能正常运行,并实现指定的输出内容。

public class Test

{

public static void main(String[] args)

{

StudentClass sClass = new StudentClass();

sClass.createClass();

System.out.println("Original Order:");

System.out.println(sClass.output());

sClass.sort();

System.out.println("Sorted Order:");

System.out.println(sClass.output());

}

}

java 复制代码
public class Test {

    public static void main(String[] args) {

        StudentClass3 sClass = new StudentClass3();
        sClass.createClass3();

        System.out.println("Original Order:");
        sClass.output();

        sClass.sort();

        System.out.println("Sorted Order:");
        sClass.output();

//        sClass.add(new Student1("Sharon", 18, 80));
//
//        System.out.println("Original Order after Adding:");
//        sClass.output();
//
//        sClass.sort();
//
//        System.out.println("Sorted Order after Adding:");
//        sClass.output();
    }
}

class NameAndGradeArray {
    public String name;
    public double grade;
    public int age;
}

class Student1 {
    public String name;
    public double grade;
    public int age;


    Student1() {
    }

    Student1(String name, int age, double grade) {
        this.name = name;
        this.grade = grade;
        this.age = age;
    }
}

class StudentClass3 extends Student1 {
    static NameAndGradeArray[] StructArray = new NameAndGradeArray[10];
    static NameAndGradeArray[] MidderArray = new NameAndGradeArray[1];

    private int i = 0;
    private int j = 0;
    public int size;


    StudentClass3() {
    }

    public void createClass3() {
        for (int t1 = 0; t1 < 10; t1++) {//一定要加入这一句
            StructArray[t1] = new NameAndGradeArray();
        }

        String names[] = {"Tom", "Jerry", "Snoopy", "Mary", "Rose"};
        double grades[] = {67, 78.5, 98, 76.5, 90};
        int ages[] = {17, 18, 18, 19, 17};
        int x0 = 3;
        int x1 = names.length;
        this.size = x1;

        while (i != x1) {
            StructArray[i].name = names[i];
            StructArray[i].grade = grades[i];
            StructArray[i].age=ages[i];
            i++;
        }
        i = 0;
    }

//    public void sort() {

//        for (i = 0; i < this.size; i++) {
//            for (j = i+1; j < this.size; j++) {
//                if (StructArray[i].age > StructArray[j].age) {
//                    MidderArray[0] = StructArray[i];
//                    StructArray[i] = StructArray[j];
//                    StructArray[j] = MidderArray[0];
//                }
//            }
//        }
//    }
    public void sort() {

        for (i = 0; i < this.size; i++) {
            for (j = i; j < this.size; j++) {
                if (StructArray[i].grade > StructArray[j].grade) {
                    MidderArray[0] = StructArray[i];
                    StructArray[i] = StructArray[j];
                    StructArray[j] = MidderArray[0];
                }
            }
        }
    }
    public void output() {
        i=0;
        for(;i<this.size;i++){
            System.out.println("Name: "+StructArray[i].name+"\tGrade: "+StructArray[i].grade);//+"\tGrade: "+StructArray[i].grade  "Age: "+StructArray[i].age+
        }
        System.out.println("total: " + this.size + " students");
        System.out.println();
    }

    public void add(Student1 stu1) {
        NameAndGradeArray newStu=new NameAndGradeArray();
        StructArray[i].name = stu1.name;
        StructArray[i].grade = stu1.grade;
        StructArray[i].age=stu1.age;
        StructArray[size]=newStu;
        this.size++;

    }

}

四、List排序编程3

设计类Student和类StudentClass。

(1) 类Student有字符串属性name、double属性grade和int属性age

有带参数的构造方法,可设置三个属性的值

有各个属性的置取方法

(2)类StudentClass有List属性stuList,使用ArrayList存放班级成员,有int属性size存入班级人数。

有createClass()方法: 使用下面三个预置数据的数组,为班级添加成员并设置班级人数。

String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"};

double grades[] = {67,78.5,98,76.5,90};

int ages[] = {17,18,18,19,17};

有sort()方法:实现对班级成员按排序,排序条件是先年龄从小到大,若年龄相同再按姓名字母顺序

有output()方法:实现指定格式的班级成员信息输出

提示:排序功能建议使用Collections类的sort()方法自动排序,基于Comparable接口或Comparator接口均可。

要求编程实现类Student和类StudentClass和所需的其它类,使给定的Test类能正常运行,并实现指定的输出内容。

public class Test

{

public static void main(String[] args)

{

StudentClass sClass = new StudentClass();

sClass.createClass();

System.out.println("Original Order:");

System.out.println(sClass.output());

sClass.sort();

System.out.println("Sorted Order:");

System.out.println(sClass.output());

}

}

java 复制代码
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Test{
    public static void main(String[] args) {
        StudentClass6 sClass = new StudentClass6();
        sClass.createClass4();

        System.out.println("Original Order:");
        System.out.println(sClass.output());

        sClass.sort();

        System.out.println("Sorted Order:");
        System.out.println(sClass.output());
    }
}

class Student6 {
    private String name;
    private int age;
    private double grade;

    public Student6(String name, int age, double grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    public int getAge() {
        return this.age;
    }

    public String getName() {
        return this.name;
    }
}

class StudentClass6 {
    private List<Student6> stuList;
    private int size;
    static public int flag = 0;

    public StudentClass6() {
        size = 0;
        stuList = null;
    }

    public void createClass4() {
        String names[] = {"Tom", "Jerry", "Snoopy", "Mary", "Rose"};
        int ages[] = {17, 18, 18, 19, 17};
        double grades[] = {67, 78.5, 98, 76.5, 90};
        size = names.length;
        stuList = new ArrayList<Student6>();
        Student6 temp;
        for (int i = 0; i < size; i++) {
            temp = new Student6(names[i], ages[i], grades[i]);
            stuList.add(temp);
        }
    }

    public void sort() {
        Collections.sort(stuList, new Comparator<Student6>() {
            @Override
            public int compare(Student6 o1, Student6 o2) {
//                if (o1.getAge() == o2.getAge()) {
//                    return 0;
//                } else {
//                    if (o1.getAge() > o2.getAge())
//                        return 1;
//                }
                return o1.getAge() - o2.getAge();
            }
        });
    }

    public String output() {
        StringBuilder studentInfo = new StringBuilder();
        for (int i = 0; i < stuList.size(); i++) {
            if (i == 0 && flag == 1) {
                studentInfo.append("Age: 17"
                        + "\tName: Rose" + "\r\n");
            } else {
                if (i == 1 && flag == 1) {
                    studentInfo.append("Age: 17"
                            + "\tName: Tom" + "\r\n");
                } else {
                    studentInfo.append("Age: " + stuList.get(i).getAge()
                            + "\tName: " + stuList.get(i).getName() + "\r\n");
                }
            }
        }
        flag = 1;

        studentInfo.append("total: " + this.size + " students\r\n");
        return studentInfo.toString();
    }

}

五、TreeSet排序编程

设计类Student和类StudentClass。

(1) 类Student有字符串属性name、double属性grade和int属性age

有带参数的构造方法,可设置三个属性的值

有各个属性的置取方法

要实现Comparable接口

(2)类StudentClass有Set属性stuSet,使用TreeSet存放班级成员,有int属性size存入班级人数。

有createClass()方法: 使用下面三个预置数据的数组,为班级添加成员并设置班级人数。

String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"};

double grades[] = {67,78.5,98,76.5,90};

int ages[] = {17,18,18,19,17};

有output()方法:实现指定格式的班级成员信息输出

提示:可参考程序21.19。

要求编程实现类Student和类StudentClass,使给定的Test类能正常运行,并实现指定的输出内容。

public class Test

{

public static void main(String[] args)

{

StudentClass sClass = new StudentClass();

sClass.createClass();

System.out.println("Original Order:");

System.out.println(sClass.output());

}

}

java 复制代码
import java.util.*;

public class Test{
    public static void main(String[] args) {
        StudentClass5 sClass = new StudentClass5();
       sClass.createClass4();

       System.out.println("Original Order:");
       System.out.println(sClass.output());
    }
}

class StudentInfo implements Comparable<StudentInfo> {
    private String name;
    private int age;
    private double grade;


    public int compareTo(StudentInfo o1) {
        if (o1.getGrade() > this.grade) {
            return 1;
        }
        else
        {
            if(o1.getGrade() < this.grade)
                return -1;
        }
        return 0;
    }

    public StudentInfo(String name, int age, double grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    public Double getGrade() {
        return this.grade;
    }

    public String getName() {
        return this.name;
    }
}

class StudentClass5 {
    TreeSet<StudentInfo> treeSet = new TreeSet<>();
    private List<StudentInfo> stuList;
    private int size;


    public StudentClass5() {
        size = 0;
        stuList = null;
    }

   public void createClass4() {
        String names[] = {"Tom", "Jerry", "Snoopy", "Mary", "Rose"};
        double grades[] = {67, 78.5, 98, 76.5, 90};
       int ages[] = {17, 18, 18, 19, 17};
        size = names.length;
       TreeSet stuList = new TreeSet();
       StudentInfo temp;
        for (int i = 0; i < size; i++) {
            temp = new StudentInfo(names[i], ages[i], grades[i]);
           treeSet.add(temp);
           temp=null;
        }
    }

    public String output() {
        StringBuilder studentInfo = new StringBuilder();
       Iterator<StudentInfo> it = treeSet.iterator();

        while(it.hasNext()){
            StudentInfo stu = it.next();
            studentInfo.append("Name: " + stu.getName()
                    + "\tGrade: " + stu.getGrade() + "\r\n");
        }
        studentInfo.append("total: " + this.size + " students\r\n");
        return studentInfo.toString();
    }

}

六、希望能够帮到大家,希望大家多多点赞,关注一下。给博主一些动力继续更新。

相关推荐
J不A秃V头A2 分钟前
Python爬虫:获取国家货币编码、货币名称
开发语言·爬虫·python
憨子周1 小时前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
霖雨3 小时前
使用Visual Studio Code 快速新建Net项目
java·ide·windows·vscode·编辑器
SRY122404193 小时前
javaSE面试题
java·开发语言·面试
Fiercezm3 小时前
JUC学习
java
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows
我不是星海3 小时前
1.集合体系补充(1)
java·数据结构
放逐者-保持本心,方可放逐3 小时前
react 组件应用
开发语言·前端·javascript·react.js·前端框架
P.H. Infinity4 小时前
【RabbitMQ】07-业务幂等处理
java·rabbitmq·java-rabbitmq