One, array
1.Java is the implementation of an array as an object, rather than a contiguous storage space
2. In Java, an array is a class that provides methods and properties, such as the number of Array.Length
The array name in 3.Java can be seen as an object reference, and the length cannot be defined directly at the time of initialization
Example: int a[3]; Method of declaring errors
4. An element in an array can also be a composite data type, when an array element is actually an object reference
Cases:
New Complex[3]; int i; for (i = 0; i < arr.length; i++) { new Complex ();}
5. Declaration of one-dimensional array: type arrayname[]; or type[] arrayname;
Dynamic initialization: type[] arrayname = new Type[length];
Static initialization: type[] arrayname = {val0, Val1, ..., valn};
6. Declaration of two-dimensional arrays: type[][]arrayname; or type arrayname[][];
Dynamic initialization: type[][] arrayname = new Type[length1][length2]; Rule array
Or: type[][] arrayname = new type[length1][]; Irregular arrays
Array[0] = new Type[length2_1];
ARRAY[1] = new Type[length2_2]; ......
Static initialization: type[][] Arrayname = {{1, 2, ...}, {1, 2, ...}, ...};
Note: Arrayname.length represents the number of rows in a two-dimensional array
Arrayname[i].length represents the number of columns in row I of a two-dimensional array
For statements in 7.foreach form
Form: for (data type iteration variable: Array | collection) {
Loop body
}
Note: The value of an element in the array cannot be modified by iterating through a variable in the loop body of the foreach form
8.Arrays Class Common methods
Serial number |
Method |
Meaning |
1 |
static int BinarySearch (type[] A, type key) |
Binary lookup returns the value of the element in the array a with the value key |
2 |
Static Boolean Equals (Type[] A, type[] b) |
Determine if arrays A and B are equal |
3 |
Static Boolean deepequals (Object[] A, object[] b) |
Determines whether arrays A and b are equal, for multidimensional arrays |
4 |
static void Fill (type[] A, type Val) |
Assigns the elements in array a a value of Val |
5 |
static void sort (type[] a) |
Non-descending ordering of elements in array a |
6 |
static void sort (type[] A, int from, int. to) |
The non-descending ordering of the elements from the lower-to-to element in array a |
7 |
Static String toString (type[] a) |
Returns the string representation of the contents of the array a |
8 |
Static String deeptostring (type[] a) |
Returns a string representation of the contents of an array, for use in multidimensional arrays |
Second, string
1. String constants that appear in Java are converted to objects of type string at compile time
2. Creating a String Object
How to construct the String class: String () string (string str) string (char[] arr)
To create a method:
(1) String str1; str1 = new String ("Hello World");
(2) String str2 = "Hello World";
(3) char[] arr = {' A ', ' B ', ' C '}; String arr3 = new string (arr);
Common methods for 3.String classes
(1) operator +: Connection operation for two strings
(2) operator charAt (int position): The position character in a string (counting from 0)
(3) operator equals (string str): Compares two strings for the same
Note:
New String ("Hello"new string ("Hello");
Str1. The result of equals (STR2) is true
The result of str1 = = Str2 is false because the "= =" operator is used to compare str1 and str2 values, and the two reference variables hold separate string object addresses
Comparison of ignoring case: Equalsignorecase ()
(4) operator indexOf (): Find string, parameter can be character or string
(5) operator split (): Used for the segmentation of a string, the string is divided according to the given parameters, the argument can be a character or a string
(6) operator tolowcase () touppercase (): Convert case
Parameters of the 4.main method
Cases:
class Argsdemo { publicstaticvoid main (string[] args) { for (int i = 0; i < args.length; i++) { System.out.println ("args[" + i + "]:" + Args[i] ); } }}
5.StringBuffer and StringBuilder class
The string class is used to handle string variables that are not changed after creation, so using StringBuffer or StringBuilder is more efficient when dealing with variable-content strings
The StringBuffer class supports multi-threading, so it uses StringBuffer
Construction Method:
New StringBuffer (); // Create an empty StringBuffer object (initial capacity is.) New StringBuffer (ten); // Create a StringBuffer object with an initial capacity of 10 New StringBuffer ("Hello"); // Create a StringBuffer object with a content of hello (initial capacity is string length Plus +) // StringBuffer s4 = "Hello"; For the illegal
6.StringBuffer class method
(1) public void append (type T); function to append new content
public void Insert (int i, type T); function to insert new content
Type types can be all basic data types, character arrays, object references, and so on
I means inserting new content in the word I prompt
(2) Conversion of the string class to the StringBuffer class
stringbuffer->string:string s2 = s1.tostring ();
String->stringbuffer:stringbuffer S1 = new StringBuffer (s2);
Third, Vector class
1. Vector classes are used to describe vectors, defined in the Java.util package, as variable-length arrays that allow different types of elements to coexist
3 properties of the vector class: length, capacity, and increment
Length: Number of elements stored in vectors
Capacity: vector size, general capacity greater than or equal to length
Increment: The amount of each expansion when the capacity is insufficient
Vector capacity expansion is automatic, that is, when the new element exceeds its capacity, the capacity of the vector will automatically grow
Construction Method:
Method |
Meaning |
Public Vector () |
Constructs an empty vector object with a capacity of 10 and an increment of 0 |
Public Vector (Collection c) |
Used to construct a vector containing the elements in the specified collection |
Public Vector (int initialcapacity) |
Vector initial capacity is initialcapacity, increment is 0 |
Public Vector (int initialcapacity, int capacityincrement) |
Vector initial capacity is initialcapacity, increment is capacityincrement |
The smaller the vector increment set, the higher the utilization of the memory space, but the greater the number of memory allocations
The larger the vector increment is set, the less memory allocation will be performed, but it is possible to generate memory waste
Adding elements to a vector: the Add () method or the Insertelementat () method
After inserting an element, the elements after the insertion position are moved backward by one unit
Delete an element in a vector: Removeelement () method
After deleting an element, the element after the delete position moves one unit forward
Clear all elements in the vector: the Removeallelement () method or the clear () method
Accessing the specified subscript element: ElementAt () method
Find the position of an element in a vector: IndexOf () method
Java Learning Notes-5. Common Data Structures