Object oriented part of java big data development (encapsulation 3)

1. Collection basis

1.1 collection overview

Features of set: provide a storage model with variable storage space, and the 1 data capacity can be changed.

There are many collection classes. At present, let's learn an ArrayList

ArrayList<E>

  • Resizable array implementation
  • <E> Is a special data type, generic

How to use?

Use the reference data type to replace where E appears

Examples: ArrayList < string >, ArrayList < student >

1.2 construction method and addition method of ArrayList

Method nameexplain
public ArrayList()Create an empty collection object
public boolean add(E e) Appends the specified element to the end of this collection
public void add(index,E element)Inserts the specified element at the specified location in this collection
package test1;

import java.lang.reflect.Array;
import java.util.ArrayList;

/*
* public ArrayList()	Create an empty collection object
public boolean add(E e)	Appends the specified element to the end of this collection
public void add(index,E element)	Inserts the specified element at the specified location in this collection
* */
public class arrayListDemo1 {
    public static void main(String[] args) {
        //public ArrayList() 	 Create an empty collection object
        ArrayList<String> array = new ArrayList<String>();
        System.out.println("Araay:"+ array);
       // public boolean add(E e) 	 Appends the specified element to the end of this collection
        array.add("Hello");
        array.add("World");
        System.out.println("Araay:"+ array);

        //public void add(index,E element) 	 Inserts the specified element at the specified location in this collection
        array.add(1,"521");
        //array.add(3,"521");IndexOutOfBoundsException set index out of bounds
        System.out.println("Araay:"+ array);
    }
}

1.3 common methods of ArrayList

Method nameexplain
public boolean remove(Obiect 0)

Delete the specified element and return whether the deletion is successful

public E remove(int index)Deletes the specified index element and returns the deleted element
public E set(int index,E element)

Modify the element at the specified index and return the modified element

public E get(int index)Returns the element at the specified index
public int size()Returns the number of elements in the collection
package test2;

import java.util.ArrayList;

/*
* public boolean remove(Obiect 0)	Delete the specified element and return whether the deletion is successful
public E remove(int index)	Deletes the specified index element and returns the deleted element
public E set(int index,E element)	Modify the element at the specified index and return the modified element
public E get(int index)	Returns the element at the specified index
public int size()	Returns the number of elements in the collection
* */
public class arrayListDemo2 {
    public static void main(String[] args) {
        ArrayList<String> array = new ArrayList<>();
        array.add("Hello");
        array.add("World");

        // public boolean remove(Obiect 0) 	 Delete the specified element and return whether the deletion is successful
        //System.out.println(array.remove("Hello"));

        //public E remove(int index) 	 Deletes the specified index element and returns the deleted element
        //System.out.println(array.remove(1));
        //IndexOutOfBoundsException
        //System.out.println(array.remove(2));

        //public E set(int index,E element) 	 Modify the element at the specified index and return the modified element
        //System.out.println(array.set(0,"1000"));
        //IndexOutOfBoundsException
       // System.out.println(array.set(2,"1000"));

        //public E get(int index) 	 Returns the element at the specified index
//        System.out.println(array.get(0));
//        System.out.println(array.get(1));
        //IndexOutOfBoundsException
//        System.out.println(array.get(2));


        //public int size() 	 Returns the number of elements in the collection
        System.out.println(array.size());

        System.out.println(array);
    }
}

1.4 case: ArrayList stores strings and traverses

Requirements: create a set of stored characters, store three string elements, and use the program to traverse the set on the console

Idea: ① create a collection of stored strings

② use the for loop to traverse the set

③ use the get method to obtain the set elements

package test3;

import java.util.ArrayList;

/*
* Requirements: create a set to store strings, store three string elements, and use the program to traverse the set on the console
* */
public class arrayListDemo3 {
    public static void main(String[] args) {
        //Create a collection of stored strings
        ArrayList<String> array = new ArrayList<>();
        array.add("Hello");
        array.add("10000");
        array.add("World");

        //Use the for loop to iterate through the collection
        for(int i=0;i<array.size();i++){
            //Use the get method to get the collection elements
            System.out.println(array.get(i));
        }
    }
}

1.5 case: storing student objects and traversing

Requirements: create a set to store student objects, store three student objects, and use the program to traverse this set on the console

Idea: ① first create a student class in which the member variables age and name are defined and modified by private. Construct a construction method without parameters in the class, and then construct a construction method with parameters. Define setxxx() method and getxxx() method

② create a collection object, then create a student object, and add the student object to the collection object

③ traverse the console and output the results in the for loop

package test4;
/*
      Student class
 */
public class Student {
    //Define member variables
    private int age;
    private String name;

    //Construction method
    //Nonparametric construction method
    public Student(){


    }
    //Structural method with parameters
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    public void setName(String name){
        this.name =name;
    }

    public String getName(){
        return name;
    }

    public void setAge(int age){
        this.age =age;
    }

    public int getAge(){
        return age;
    }
}
package test4;

import java.util.ArrayList;

/*
Requirements: create a set to store student objects, store three student objects, and use the program to traverse this set on the console
* */
public class arrayListDemo4 {
    public static void main(String[] args) {
        //Create collection object
        ArrayList<Student> studentArrayList = new ArrayList<Student>();
        //Create student object
        Student s1 = new Student("Wu Yanzu",58);
        Student s2 = new Student("Lin Qingxia", 40);
        Student s3 = new Student("Zhang Guorong", 50);
        //Use the add method to add the student object to the collection
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        //Use the for loop to traverse the collection
        for(int i=0;i<studentArrayList.size();i++){
           Student s=studentArrayList.get(i);
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}

1.6 case: store the student object and traverse the upgraded version

Requirements: create a set to store student objects and store three student objects. Use the program to traverse this set on the console. The name and age of students are entered from the keyboard.

Idea: ① first create a student class in which the member variables age and name are defined and modified by private. Construct a construction method without parameters in the class, and then construct a construction method with parameters. Define setxxx() method and getxxx() method

② create a set object, then create a Scanner object, get input from the keyboard, create a student object, and assign the student attribute by passing the parameters entered by the keyboard using the {setxxx() method. Finally, add the student object to the collection. However, in order to improve the reusability of the code, a method is defined here

③ use the for loop to traverse the collection and output the results on the console

package test5;
/*
     Student class
 */
public class Student {
    private int age;
    private String name;

    public Student(){}

    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }

    public void setAge(int age){
        this.age=age;
    }

    public int getAge(){
        return age;
    }

    public void setName(String name){
        this.name=name;
    }

    public String getName(){
        return name;
    }

}
package test5;

import java.util.ArrayList;
import java.util.Scanner;

/*
* Requirements: create a set for storing student objects, store three student objects, and use the program to traverse the set on the console,
* The student's name and age are entered from the keyboard.
* */
public class arrayListDemo5 {
    public static void main(String[] args) {
        //Create collection object
        ArrayList<Student> array = new ArrayList<>();
        /*
        //Create a Scanner object and get input from the keyboard
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter student name: ";
        String name = sc.nextLine();
        System.out.println("Please enter student age: ";
        int age = sc.nextInt();
        //Create student object
        Student s = new Student();
        s.setAge(age);
        s.setName(name);*/
        //Call method
        addStudent(array);
        addStudent(array);
        addStudent(array);

        //Use the for loop to traverse the collection
        for(int i=0;i<array.size();i++){
            Student s = array.get(i);
            System.out.println(s.getName()+","+s.getAge());
        }
    }
    //Definition method
    public static void addStudent(ArrayList<Student>array){

        //Create a Scanner object and get input from the keyboard
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter student name:");
        String name = sc.nextLine();
        System.out.println("Please enter student age:");
        int age = sc.nextInt();
        //Create student object
        Student s = new Student();
        s.setAge(age);
        s.setName(name);
        array.add(s);
    }
}

2. Student management system

① Write student class

package studentManagerSystem;
/*1.Create student class

 */
public class Student {
    //Define member variables
    private String name;
    private int age;
    private String sid;
    private int classroom;
    //Define the parameterless construction method alt+insert and select it according to your needs
    public Student() {
    }
    //Define a construction method with four parameters
    public Student(String name, int age, String sid, int classroom) {
        this.name = name;
        this.age = age;
        this.sid = sid;
        this.classroom = classroom;
    }
    //set() and get() methods


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String number) {
        this.sid = number;
    }

    public int getClassroom() {
        return classroom;
    }

    public void setClassroom(int classroom) {
        this.classroom = classroom;
    }
}

② Write main interface

  1. Create a collection object to receive student objects
  2. Write the main page with the output statement
  3. Complete keyboard entry with Scanner
  4. Use the switch statement to realize the selection of operation
  5. Use the while loop to complete and return to the main page again

③ Write and add student methods

  1. Create a Scanner object and get student information input from the keyboard
  2. Use the set method of the student object to pass the parameter to the student member variable
  3. Use the add method of the collection to add the student object to the collection
  4. Output prompt: Student added successfully
  5. Call the isUsed method and assign the return value to flag
  6. If flag is true, the prompt is output; otherwise, break
  7. Use the while loop to implement the program. Come back here

④ Write the method to judge whether the student number is repeated

  1. Determine whether the method needs to return value and pass parameter type
  2. Set a variable of boolean type and assign the value to false
  3. Use the for loop to traverse the collection. If the student number of the student object in the collection is consistent with the input student number, modify the variable flag to true
  4. Return flag

④ Write and delete student methods

  1. Create a Scanner object and get the student number to be deleted from the keyboard
  2. Use the for loop to traverse the collection. Judge that when the input student number is the same as the student number of the elements in the collection, use the remove method of the collection object to delete it. When the input student number does not exist, output: the student number you entered is incorrect.

⑤ Compiling and modifying student methods

  1. Create a Scanner object and get the student number to be modified from the keyboard
  2. Use the for loop to traverse the set. When the input student number is the same as the student number of the elements in the set, output the prompt to let the user input the new name, age and class. Use the set method to assign these attributes to the member variables. Use the set method of the set object to add the modified Student object to the set.

⑥ Write and view all student methods

  1. Use the if statement to make judgment when collecting objects When size() = = 0, output: Please add an object first and use the return statement to end the method.
  2. If the element in the collection object is not empty, use the output statement to output a print format.
  3. Use the for loop to traverse the collection and use the collection object get gets the collection elements. Through student get () gets the values of each member variable and prints them out.  
package studentManagerSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        //Create collection object
        ArrayList<Student> array = new ArrayList<>();
        //Loop back to the main page
        while (true) {
            //2. Prepare main page
            System.out.println("---------Welcome to student management system----------");
            System.out.println("1 Add student");
            System.out.println("2 Delete student");
            System.out.println("3 Modify student");
            System.out.println("4 View all students");
            System.out.println("5 sign out");
            //Get input from keyboard
            Scanner sc = new Scanner(System.in);
            System.out.println("Please select the action you want to perform(1-5): ");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    addStudent(array);
                    break;
                case 2:
                    removeStudent(array);
                    break;
                case 3:
                    setStudent(array);
                    break;
                case 4:
                    lookStudent(array);
                    break;
                case 5:
                    System.out.println("Thank you for using");
//                    break;
                    System.exit(0);//JVM exit
            }
        }
    }
    //Write and add student methods
    public static void addStudent(ArrayList<Student> array){
        //Using while loop implementation
        //Create a Scanner object and enter student information from the keyboard
        Scanner sc = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("Please enter student ID:");
            sid = sc.nextLine();
            boolean flag = isUsed(array, sid);
            if (flag) {
                System.out.println("The student number you entered has been used. Please re-enter it");
            }else{
                break;
            }
        }

        System.out.println("Please enter student name:");
        String name = sc.nextLine();
        System.out.println("Please enter student age:");
        int age = sc.nextInt();
        System.out.println("Please enter student class:");
        int classroom = sc.nextInt();


        //Create student object and receive student information
        Student s1 = new Student();
        s1.setName(name);
        s1.setClassroom(classroom);
        s1.setAge(age);
        s1.setSid(sid);
       //Add students to collection
        array.add(s1);
        //Output prompt
        System.out.println("Student added successfully");

    }
    //Write and judge whether the student number is repeated
    public static boolean isUsed(ArrayList<Student> array,String sid){
         boolean flag = false;
         //Traversal set
        for (int i=0;i<array.size();i++){
            Student s = array.get(i);
            if(s.getSid().equals(sid)){
                flag = true;
            }
        }

         return flag;
    }
    //Write and delete student methods
    public static void removeStudent(ArrayList<Student> array){
          //Get the student number of the student you want to delete from the keyboard
        System.out.println("Please enter the student ID to delete:");
        Scanner sc = new Scanner(System.in);
        String sid = sc.nextLine();
        //Traverse the collection and delete the element
        for (int i=0;i<array.size();i++){
            Student s = array.get(i);
            if(s.getSid().equals(sid)){
                array.remove(i);
                System.out.println("Delete student succeeded");
            }else{
                System.out.println("Student id not found");
            }

        }

    }
    //Compiling and modifying student methods
    public static void setStudent(ArrayList<Student> array){
        //Obtain the student number of the student to be modified from the keyboard
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the student number of the student to be modified:");
        String sid = sc.nextLine();
        //Traverse the set and find the element consistent with the student number entered
        for (int i=0;i<array.size();i++){
            Student s = array.get(i);
            if(s.getSid().equals(sid)){
                System.out.println("Please enter the modified Student Name:");
                String name = sc.nextLine();
                s.setName(name);
                System.out.println("Please enter the modified student age");
                int age = sc.nextInt();
                s.setAge(age);
                System.out.println("Please enter the modified student class");
                int classroom = sc.nextInt();
                s.setClassroom(classroom);
                array.set(i,s);
                System.out.println("Modify student successfully");
            }else{
                System.out.println("Student id not found");
            }
        }

    }
    //Edit view all students
    public static void lookStudent(ArrayList<Student> array){
        //Add a judgment module. If there are no elements in the set, output a prompt
        if(array.size()==0){
            System.out.println("Please add students first");
            return;
        }
        System.out.println("Student number\t\t full name\t\t Age\t\t class");
        //Traversal set
        for (int i=0;i<array.size();i++){
            Student s = array.get(i);
            System.out.println(s.getSid()+"\t\t"+s.getName()+"\t\t"+s.getAge()+"\t\t"+s.getClassroom());
        }
    }
}

 

Keywords: Java Big Data

Added by brainstem on Wed, 09 Feb 2022 12:33:12 +0200