Third, Java Array
Main content: Array overview, one-dimensional array declaration, reference to array elements, default initialization of array elements, creation of arrays, array initialization, multidimensional arrays, multidimensional array initialization, array ordering
1. Array Overview
An array is a combination of multiple identical types of data that enables unified management of these data.
The elements in the array can be any data type, including the base type and the reference type.
Arrays are reference types and can be understood as objects (object), and each element in an array is equivalent to a member variable of that object.
Once an array is initialized, the length is immutable.
2. one-dimensional array declaration
How one-dimensional arrays are declared: type var[] or type[] var;
For example:
int a[];
Int[] A1;
Double b[];
MyDate []c; Object Array
When declaring an array individually, you cannot specify its length ( The number of elements in the array ), for example: int a[5]; Illegal
3. references to array elements
You can use the keyword new to create an array object in Java
Defines an element in an array before it is allocated with the operator new ;
Each array has a property of length indicating its lengths, for example:a.length indicates The length of the array a ( Number of elements )
How array elements are referenced: array name [ array element subscript ]
An array element subscript can be an integer constant or an integer expression. such as a[3], b[i], C[6*i]
Array element subscript starting from 0 ; array of length n valid subscript range : 0 to n-1
4. default initialization of array elements
An array is a reference type whose elements are equivalent to the member variables of a class, so after allocating space for the array, each of these elements is implicitly initialized in the same way as member variables. For example:
public class Test {
public static void Main (String argv[]) {
int a[]= new INT[5];
System.out.println (A[3]); the default value for A[3] is 0
}
}
5. creating an array
creating an Array object using the keyword new in Java
Int[] Scores=new int[]{99,88,77,55,22};
* Allocate space
Syntax: array name =new data type [ array length ]
Example:int scores[];
Scores=new Int[5];
Merge:int scores[]=new int[5];
6. Initialization of arrays
Dynamic initialization: The array definition is performed separately from the operation of allocating space and assigning values to the elements.
Static initialization: Allocates space and assigns values to the array elements while defining the arrays.
Supplemental: array name . length is used to get the array length
* output scores elements of an array
for (i=1;i<=scores.length;i++) {
System.out.println (scores[]);
}
7. Two-dimensional arrays
Declaring and allocating space: data type [] array name =new data type [ number of rows ] [ number of columns] ]
Int[][] Num=new int[2][3];
Or:int[][] num;
Num=new Int[2][3];
8. using the Arrays class to manipulate arrays in Java
(Use Arrays class must use import java.util.Arrays;
public class account{} Import Package)
(1) Sort by: Arrays.sort ( array name ) ;
(2) To convert an array to a string: arrays.tostring ( array name ) ;
(3) foreach operation array: syntax:for ( element type element variable: Traverse object )
{ execute code }
9. Bubble Sort
Basic syntax Examples:
public static void Pubblesort (int[] array) {
int temp;
for (int x = 0; x < Array . length -1; x ++ ) {for(int y = 0; y < Array . length -1- x ; y ++) {
if (array[y] > array[y+1]) { temp = array[y];
Array [y] = Array [y+1];
Array [y+1] = Temp ;
}
}
}
for (int z = 0; z < array.length; z++) {
System. out. Print (array[z]+ "");
}
}
10.for-each statement (used to traverse all elements in an array)
General syntax: for( data type variable: traversing object ) {
Execute statement }
Example:
General access to an array using the format:
int sum=0;
Int[] nums={1,2,3,4,5,6,7,8,9};
for (int i=0;i<nums.length;i++) {
Sum+=nums[i];
}
Use the For-each statement to traverse:
int sum=0;
Int[] nums={1,2,3,4,5,6,7,8,9};
for (int i:nums) {
Sum+=i;}
To access the multidimensional array syntax:
int nums[][]=new int[5][5];
for (int x[]:nums)
for (int y:x)
working with elements
Iv. Classes and objects
Main content:
Classes and objects, encapsulation and hiding of information, construction methods, overloading of methods, The This keyword,package/import Statements
Object-oriented three features: encapsulation, inheritance, polymorphism
1. class and Object : The core concept of an object-oriented approach. A class is a description of a kind of thing, an abstract, conceptual definition; An object is an individual of the actual existence of such a thing, and thus is also called an instance (instance).
2. define the class:
Syntax format:
[< modifier ;] class < class name > {
[< attribute ;]
[< Constructor ;]
[< method ;]
}
Description: Modifier public : class can be arbitrarily accessed
The body of the class is enclosed in {} .
Example:
public class person{
private int age; declaring a private variable age
public void setage (int i) {// defines the common method setage
age = i;
}
}
3. objects cancorrespond to the static and dynamic properties of a thing, respectively, by the property "attribute" and the Method "Methods".
4. Properties--various characteristics of an object
To define a property:
Syntax format:
[< modifier ;] type < Property name > [= initial value ];
Description: Modifier private: This property can only be accessed by methods of this class.
Modifier public : This property can be accessed by methods other than this class.
Type: Any base type, such as int,boolean , or any class.
Example:
public class person{
private int age; declaring The private variable age
Public String name ="Jack"; declaring the public variable name
}
Properties are sometimes referred to as: data members ( data ), member variables ( variables )
5. define the method:
Syntax format:
< modifier > < return type > < method name > ([< parameter table ]) {
[< statement ;]
}
Description: modifier:public,private,protected and so on.
return type: return statement passing the returned value. No return value:void.
Example:
public class person{
private int age;
public int getage () {return age;}
public void setage (int i) {
age = i;
}
}
6. creation and use of objects
Create a new object using the new+ construction method;
Use the object name . Object Members "to access object members (including properties and methods);
After creating a new object , We can use the object name . The format of the object member to access the members of the object.
Example:
Class Testperson
{
public static void Main (string[] args)
{
person P1 = new person ();
person P2 =new person ();
P1.age = 20;
P1.shout ();
P2.shout ();
}
}
Java Summary second//array and object-oriented