My fourth day of learning Java in Shanghai LeByte

Definition of
array An array is an ordered collection of data of the same type.
An array describes several data of the same type, arranged in a certain order
. Each data is called an array element, and each array element can be accessed through a subscript.
The declaration and creation of an array
must first Declare array variables to use the array in the program, the following is the syntax for declaring array variables
int[] num;//首选方法 int num[];//效果相同,但不是首选方法

  • 1
  • 2

The java language uses the new operator to create an array, the syntax is as follows:
int[] nums = new int[10];

  • 1

The elements of the array are accessed by index. The index of the array starts from 0. The
length of the array can be obtained through the array.length method.
Three kinds of initialization,
static initialization
int [] num = {1,2,3,4,5}; Man[] mans = {new Man(1,1),new Man(2,3)};

  • 1
  • 2

Dynamic initialization
int [] num = new int[10]; num[0]=1; num[1]=2;

  • 1
  • 2
  • 3

Default initialization of the array

The array is a reference type, and its elements are equivalent to the instance variables of the class. Therefore, once space is allocated for the array, each element in it is implicitly initialized in the same way as the instance variable
. The four basic characteristics of
the array and its length are determined. Once the array is created, its size cannot be changed.
The elements must be the same type, mixed types
are not allowed . The elements in the array can be any data type, including basic types and reference types.
Array variables are reference types, arrays can also be regarded as objects, and each element in the array is equivalent to a member variable of the object. The array itself is an object. In Java, the object is in the heap. Therefore, whether the array saves the original type or other object types, the array object itself is in the heap.
Array boundary
The legal range in the following table: [0.length-1], if it crosses the boundary , an error will be reported;
public static viod main(String[] args){ int[] num = {1,2,3,4,5}; System.out,println(num[5]); //会报错 }

  • 1
  • 2
  • 3
  • 4
  • 5

summary:

Array is an ordered collection of the same data type (data type can be any type)
Array is also an object. An object member variable corresponding to the array element of
the array length is determined, immutable, if out of range, then the error;
array using
ordinary For loop
For-Each loop
arrays for methods into the reference
array as the return value
Arrays class
tools array java .util.Arrays
Because the array object itself does not have any method for us to call, but the API provides a tool class for us to use, so that we can perform some basic operations on the data object.
Check the JDK help documentation
. The methods in the Arrays class are static methods modified by static. When in use, you can directly use the ll class name to call instead of using objects (note: it is not necessary, not impossible). It
has the following common functions

Assign value to the array: use the fill method to
compare the array sort: use the sotr method, in ascending order.
Compare arrays: compare whether the elements in the array are equal through the equals method.
Find array elements: use the binartSearch method to perform binary search and send operations on the sorted array.
Process-oriented & object
-oriented process - oriented thinking

The steps are clear and simple, what to do in the first step and what to do in the second.
Facing process is suitable for dealing with some simpler problems.
Object-oriented thinking

The thinking mode of clustering and categorizing things, thinking about what categories are needed to solve the problem first, and then thinking about these categories individually, and finally, thinking about the details of a certain category.
Object-oriented is suitable for dealing with complex problems and suitable for processing needs. collaboration problems than
what is an object oriented
object oriented programming {oop}
oriented programming is the nature of the object: to organize class code to the target tissue (package) of data
abstraction
three characteristics

Encapsulation,
inheritance
, polymorphism,
from an epistemological perspective, there are objects before classes. Objects are concrete things. Class is abstract. It is the abstraction of the object.
From the perspective of code operation, it is an object after the existing class. Classes are templates for objects.
Review methods and deepen
method definitions

Modifier
Return type
break: Jump out of switch, end the loop and the difference between return
Method name: Pay attention to the specification to be ok, see the name know the meaning
Parameter list: (parameter type, parameter name)
exception thrown:
method call: recursion

Static methods
Non-static methods
Formal parameters and actual parameters
Value transfer and reference transfer
this keyword
The relationship between classes and objects
is an abstract data exhaustion. It is an overall description/definition of a certain class of things, but bb does not represent A specific thing

Animals, plants, mobile phones, computers,
Person class, Pet class, Car class, etc. These classes are used to describe and define the characteristics and behaviors of a certain type of concrete
objects. Objects are concrete examples of abstract concepts

Zhang San is a concrete example of a person, and Wangcai in Zhang San’s family is an example of a dog. It
can reflect the characteristics and show the function is a concrete instance, not an abstract concept.
Create and initialize the object
using the new keyword to create When an object is
created using the new keyword, in addition to allocating memory space, the created object is also initialized by default and called
the constructor in the constructor. The constructor in the class is also called the constructor method, which actually creates the object It must be called. And the constructor has the following two characteristics

1. Must be the same as the name of the class
2. There must be no return type, and void cannot be written

Guess you like

Origin blog.csdn.net/dirft_lez/article/details/109214285