zoukankan      html  css  js  c++  java
  • java中有关类数组定义问题

    package ahaAlgorithm.chapter1;
    
    import java.util.Scanner;
    
    /**
     * 
     * @Title: BubbleSort1.java
     * @Package ahaAlgorithm.chapter1
     * @Description: TODO(类在排序中的应用)
     * @author YoungSone
     * @date 2020年7月29日 上午10:03:09
     * @version V1.0
     */
    public class BubbleSort1 {
        private static Scanner in;
    
        public static void main(String[] args) {
    
            Student t = new Student();
            int i, j, n, score;
            String name;
            in = new Scanner(System.in);
            System.out.println("有多少人参加?");
            n = in.nextInt();
            Student[] student = new Student[n];//定义Student类数组
            for (i = 0; i < n; i++) {
                student[i]=new Student(); //new Student类
                student[i].setName(in.next());
                student[i].setScore(in.nextInt());
            }
            for (i = 0; i < n - 1; i++) {
                for (j = 0; j < n - 1 - i; j++) {
                    if (student[j].getScore() > student[j + 1].getScore()) {
                        t = student[j];
                        student[j] = student[j + 1];
                        student[j + 1] = t;
                    }
                }
            }
            for (Student s : student) {
                System.out.println(s.getName() + " " + s.getScore());
            }
    
        }
    }
    class Student{
        private String name;
        private int score;
        
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getScore() {
            return score;
        }
        public void setScore(int score) {
            this.score = score;
        }
        
    }
    View Code

    java中关于类数组要进行两次new定义

    Student student =new Student[n] //声明student数组

    sutdent[i]=new student()//声明 student(i)为一个新的类student

    第一次定义:

    student[1] student[n]

    第二次定义

    student[1]        student[n]

    student类

    变量 score 和 name

    student类
  • 相关阅读:
    IL查看泛型
    IL查看委托
    IL查看override
    为VS集成IL环境
    HashTable Dictionary HashMap
    C#集合类
    Resharper团队协作之TODO
    怪物彈珠Monster Strike 攻略
    [Editor]Unity Editor类常用方法
    C# Reflection BindingFlags
  • 原文地址:https://www.cnblogs.com/YoungSone/p/13396170.html
Copyright © 2011-2022 走看看