2021-09-27

java object oriented and array

array

1. What is

An array is a collection of elements of the same data type. In Java, an array is a reference type, that is, an array is an object. The data types here include not only eight basic data types, but also reference data types. For example, arrays can be stored in arrays, and strings can be stored in arrays.

2. Writing

To use an array in a program, you must first declare an array variable. The following is the syntax for declaring an array variable:

dataType[] type=new dataType[10];

public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5};

​ // print all array elements

​ for (int i = 0; i < myList.length; i++) {

​ System.out.println(myList[i] + " ");

}

// Calculate the sum of all elements

double total = 0;

for (int i = 0; i < myList.length; i++) {

​ total += myList[i];

}

System.out.println("Total is " + total);

​ // Find the largest element

double max = myList[0];

for (int i = 1; i < myList.length; i++) {

​ if (myList[i] > max) max = myList[i];

}

System.out.println("Max is " + max); }

object oriented

1. What is

Java is an object-oriented programming language, and objects are the core of object-oriented programming. The so-called object is the entity in the real world, and there is a one-to-one correspondence between objects and entities, that is to say, each entity in the real world is an object, and it is a specific concept. Objects have the following characteristics: Objects have properties and behaviors. Objects have changing states.

2. Create objects

Objects are created from classes. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:

  • Declaration : declares an object, including the object name and object type.
  • Instantiate : Use the keyword new to create an object.
  • Initialization : When an object is created using new, the constructor is called to initialize the object.

3. Source file declaration rules

In the last part of this section, we will study the declaration rules for source files. Pay special attention to these rules when you define multiple classes in a source file, and also have import and package statements.

  • There can only be one public class in a source file
  • A source file can have multiple non-public classes
  • The name of the source file should be the same as the class name of the public class. For example: the class name of the public class in the source file is Employee, then the source file should be named Employee.java.
  • If a class is defined in a package, the package statement should be the first line of the source file.
  • If the source file contains import statements, they should be placed between the package statement and the class definition. If there is no package statement, the import statement should come first in the source file.
  • The import statement and the package statement are valid for all classes defined in the source file. Within the same source file, different package declarations cannot be given to different classes.

There are several access levels for classes, and there are different types of classes: abstract classes, final classes, and so on. These will be covered in the Access Control chapter.

In addition to the types mentioned above, Java also has some special classes, such as: inner classes , anonymous classes .

4. Source code example

//————————————————————————

/**

* Student management

* alt+shift+l automatically completes the preceding code through the following code

*/

public class StudentManage {

​ private static Student[] stuData = new Student[15];

​ private static int index = 0;

​ //add

​ public void add(Student stu) {

StuData [index] = stu;

​ index++;

​ }

​ // Check a single data

​ public void findById(int id) {

​ for (int i = 0; i < index; i++) {

​ Student student = stuData[i];

​ if (student.getId() == id) {

​ System.out.println(student);

​ break;

​ }

​ }

​ }

​ // find all

​ public void findAll() {

​ for (int i = 0; i < stuData.length; i++) {

​ Student student = stuData[i];

​ System.out.println(student);

​ }

​ }

​ // delete

​ public void del(int id) {

Integer subindex = null;

​ for (int i = 0; i < stuData.length; i++) {

​ Student student = stuData[i];

​ //Move the deleted data

​ if (delindex != null && student != null) {

StuData [delindex] = stuData [i];

​ stuData[i] = null;

Subindex ++;

​ }

​ //Specific delete method

​ if (student != null && id == student.getId()) {

​ stuData[i] = null;

​ index–;

Subindex = i;

​ }

​ }

​ }

​ // delete

​ public void del2(int id) {

​ //Create a new array to accept the deleted data

​ Student[] students = new Student[stuData.length];

​ //Create a new subscript to record the number of storage

​ int newindex=0;

​ //Convenient for loop, assign other data to new array

​ for (int i = 0; i < stuData.length; i++) {

​ Student student = stuData[i];

​ //The current object is not empty and the current object id is not the id to be deleted

​ if(student!=null&&student.getId()!=id){

​ students[newindex]=student;

​ newindex++;

​ }

​ }

StuData = students;

​ index=newindex;

​ }

​ // change

​ public void update(Student stu) {

​ }

​ public static void main(String[] args) {

​ StudentManage manage = new StudentManage();

​ manage.add(new Student(1, "Wu Liang", "Male", "0001", 20));

​ manage.add(new Student(2, "Meng Xiao", "Female", "0002", 18));

​ manage.add(new Student(3, "Jade Dragon", "Male", "0003", 20));

​ manage.add(new Student(4, "Ningning", "Female", "0004", 18));

​ manage.add(new Student(5, "Yangyang", "Male", "0005", 20));

​ manage.add(new Student(6, "Pengju", "Female", "0006", 18));

​ manage.add(new Student(7, "Leilei", "Male", "0007", 20));

​ manage.findAll();

​ manage.findById(2);

​ System.out.println("--------------------------------------------");

​ manage.del2(4);

​ manage.findAll();

}

}

//——————————————————————————

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325383487&siteId=291194637