Java programming task-driven tutorial study notes four

Table of contents

1. One-dimensional array

 Two, multidimensional array

3. Common system methods for arrays

         1. Array copy

         2. The static method provided in the tool class java.util.Arrays

4. String

        1. String class

        2. StringBuffer class

        3. String Tokenizor class

5. Common operations of arrays

         1. Array traversal

         2. The maximum value of the array

         3. Array sorting


1. One-dimensional array

1. Array definition: An array is an ordered collection of data of the same type; an array is an object. An array contains a set of variables, these variables are usually called array elements, and the number of array elements is called the array length.

2. Creation and initialization of one-dimensional array

(1) Format definition array:

data type [ ] data name;

(2) When declaring an array, it does not match the memory space. You must use the new operator to allocate memory space for it, and assign an initial value of its data type to each element in the array.

Format: array name=new data type[size];

Example: int[]x=new int[100];

The above statement is equivalent to defining 100 variables of int type in the memory, the name of the first variable is int[100], the name of the second variable is x[1], and so on, the 100th variable is The name is x[99], and the initial value of these variables is 0.

(3) In Java, in order to obtain the length of the array conveniently, a length attribute is provided to obtain the length of the array (number of elements): "array name.length".

[Case 4-1] Array creation and assignment 

 

 (4) When the array is successfully created, the array elements will be automatically assigned a default value. (When using an array, if you don't want to use the default initial value, you can also explicitly assign values ​​​​to the array elements)

Array default initialization value
type of data Default initialization value
bybe,short, int,long 0
float  double 0.0

char

A null character, i.e. '/u0000'
boolean speak
reference data type null, indicating that the variable does not refer to any object

(5) The static initialization of the array is to assign a value to each element of the array while defining the array.

Format: data type [] array name={v1,v2,v3,---,vn};

[Case 4-2] Array static initialization

 (6) Array dynamic initialization

Format: data type[] array name =new data type[]{v1,v2,v3,---,vn};

3. Array access

(1) The array reference method is: array name [index].

(2) The value of the subscript index starts from 0 to the length of the array minus 1. (Exceeding the coordinates, the abnormal ArrayIndexOutOfBoundsException is displayed when running)

[Case 4-3] Array out-of-bounds exception

 [Case 4-4] Null pointer exception

 Two, multidimensional array

A multidimensional array is a nesting of arrays, that is, an array of arrays.

1. Definition of two-dimensional array

Format: data type [ ] [ ] array name;

2. Initialize a two-dimensional array with new

Format: data type [ ] [ ] array name=new data type [number of rows] [number of columns];

3. Determine the number of rows of the two-dimensional array and the number of elements in each row by assigning the subscript specified by the initial value. type [ ] [ ] array name={{list1},{list2},... ,{listn}};

3. Common system methods for arrays

1. Array copy

(1) The class java.lang.system provides the static method arraycopy() for array copying:

public static void arraycopy(Object src,int src_ position,Object dst_.int dst_position,int length)。

(2) Copy from the src_position of the source array src to the dst_position of the destination array dst, and the copy length is length.

[Case 4-5] Array copy

 

 2. The static method provided in the tool class java.util.Arrays

(1) Sorting method

public void sort(Object[ ] a)

(2) Array lookup method.

public int binarySearch(Object [ ] a,Object key)

(3) The comparison method of the array.

public boolean equals(type [ ] a,type[ ] b)

4. String

Java provides String, StringBuilder, StringBuffer and other classes to create and manipulate string objects.

1. String class

The methods in the String class focus on query operations such as string comparison, character positioning, and substring extraction.

(1) The constructor in the String class

public String( );

For example:

// Create an empty string

String s1 =new String ( );

//Create a new String object using an existing string constant

String s2 =new String ("Hello");

//A string can be created by passing an array of strings to the constructor

char chars [ ]={'a','b','c'};

String s3=new String (chars);

//Assign by string constant

String s4="Chinese";

(2) Common methods in the String class.

1. How to get the string length

public int length(): Get the length of the string

For example:

String s="Java language";

int len =s.length( );

2. Data extraction method. Get a character or a substring from a string.

public char charAt(int index): returns the character at the specified position in the string.

public String substring (interesting beginIndex, int endIndex): returns the substring from beginIndex to endIndex-1 in the string.

public String substring (int beginIndex): returns the substring from beginIndex to the end of the string in the string

For example:

String s="Java language";

char c=s.charAt(2);//c is the character 'v'

String s1=s.substring(3,5);//s1 is the string "a language"

String s2=s.substring(3);//s2 is the string "a language"

3. Find the method. Refers to the position of searching for a certain character or a certain string from a string.

public int indexOf(int ch), returns the position of the first occurrence of the specified character ch in the string.

public int indexOf(String str): Returns the position where the specified substring str appears for the first time in the string. If the specified substring cannot be found, the method will return -1.

public int lastIndexOf(int ch) and int lastIndexOf(String str): Return the position of the last occurrence of the specified character ch or substring str in the string.

For example:

String s = "Java Language";

intpos1=s.indexOf('a');//pos1为1

int pos2 = s.indexOf("a language");//pos2 is 3

int pos3=s.lastIndexOf("a");//pos3为3

4. Comparison method.

public boolean equals(Object obj)

public booean equalsIgnoreCase(Objest obj)

publicinterestingcompareTo(String str)

publicinterestingcompareToIgnoreCase(String str)

5. Modification method

6. Segmentation method

7. Conversion method

2. StringBuffer class

3. String Tokenizor class

5. Common operations of arrays

1. Array traversal

Array traversal: visit each element in the array in turn

[Case 4-6] for loop to traverse the array

 

 2. The maximum value of the array

[Case 4-7] Get the maximum value of the elements in the array

 

 

 3. Array sorting

Bubble sort: compare two adjacent elements in the array, the smaller one goes forward, the larger one goes backward

[Case 4-8] Bubble sort

 

 

Guess you like

Origin blog.csdn.net/TAO1031/article/details/122797699