Java frequently asked interview content arrays declarations initializations bubbles multidimensional arrays sparse arrays
- 2021-11-01 03:19:43
- OfStack
Directory Array Array Declaration Creation Array Initialization Array 4 Basic Characteristics Array Boundary Multidimensional Array Arrays Class Sparse Array Summary
Array
Array is an ordered collection of data of the same type An array describes several data of the same type, which are arranged and combined in the order determined by 1 Each piece of data is called an array element, and each array element can be accessed by a subscript.
Array declaration creation
Array variables must be declared before you can use arrays in your program. The following is the syntax for declaring array variables.
da taType [] arrayRefVar//Preferred method dateType arrayRefVar []//Same effect, but not preferredThe java language uses the new operator to create arrays with the following syntax:
dateType[] arrayRefVar = new dataType[arraySize]The elements of the array are accessed through an index, which starts at 0
Get array length
arrays.length
Array initialization
Static initialization int[] a = {1,2,3,4}; Man[] mans = {new Man(1,1), new Man(2,2)}; Dynamic initialization Int[] a = new int[2]; a[0] = 1; a[1] = 2; Default initialization Array is a reference type, and its elements are equivalent to instance variables, so Array 1 is allocated space, and every element in Array 1 is vaginally initialized in the same way as instance variables.
Four Basic Characteristics of Arrays
The length of the array is determined. Once an array 1 is created, its size cannot be changed The elements of the array must be of the same type. Mixed types are not allowed The elements in an array can be any data type, including base type and reference type Array variables are of reference type, and arrays can also be regarded as being in the heap, so whether the array holds the original type or other object types, the array object itself is in the heap.
Array boundary
Legal range of subscript: "0, length-1". If it crosses the range, an error will be reported ArrayIndexOutOfBounds Exception: Array subscript out-of-bounds exception Summary Arrays are ordered collections of the same data type Arrays are also objects. Array elements are equivalent to member variables of objects. Array length is definite and immutable. If the boundary is crossed, an error "ArrayIndexOutOfBoundsException" is reported
Multidimensional array
Multidimensional arrays can be regarded as arrays of arrays
//2 Dimensional array Two lines 5 Column
int a[][] = new int[2][5];
package com.sxl.array;
public class Demo04 {
public static void main(String[] args) {
//2 Dimensional array
int[][] array = {{1,2},{3,4},{5,6}};
for (int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++){
System.out.println(array[i][j]);
}
}
}
}
Class Arrays
Array tool classjava.util.Arrays
Arrays
The methods in the class are all static static methods, which can be called directly by using the class name
Sort arrays:
sort
Method.
Compare arrays: through
equals
Method compares the elements in an array for equality
package com.sxl.array;
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
// Static initialization: Create Assignment
int[] array = {1,2,3,4,5};
// Dynamic initialization : Include default initialization
int[] b = new int[10];
b[0] = 10;
//printArray(array);
// printString(array);
System.out.println("==============");
int[] result = reverse(array);
printArray(result);
}
public static int[] reverse(int[] array){
int[] result = new int[array.length];
for (int i = 0,j = result.length-1; i < array.length ; i++,j--) {
result[j] = array[i];
}// Join Java Develop and exchange samples: 5931423281 Blowing water and chatting
return result;
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("=================");
// Enhancement cycle
for (int arr: array) {
System.out.print(arr+ " ");
}
System.out.println();
System.out.println("=================");
String result = Arrays.toString(array);
System.out.println(result);
}
public static void printString(int[] array){
String result = Arrays.toString(array);
System.out.println(result);
for (int i = 0; i < array.length; i++) {
if (i == 0){
System.out.print("[" +array[i] +", ");
}else if (i == array.length - 1){
System.out.print(array[i]+"]");
}else
System.out.print(array[i]+", ");
}
}
}
package com.sxl.array;
public class Demo02 {
public static void main(String[] args) {
new Demo02();
int[] array = {1,2,5,3,9,7,6,3,2};
sort(array);
}
// Bubble sorting
public static void sort(int[] array){
int temp = 0;
for (int i = 0; i < array.length; i++) {
boolean flag = false;
for (int j = 0; j < array.length-1; j++){
if (array[j+1]<array[j]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = true;
}
}
if (flag == false){
break;
}
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
}
package com.sxl.array;
public class Demo03 {// Join Java Develop and exchange samples: 5931423281 Blowing water and chatting
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
// Print all data elements
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
// Calculate the sum of all data elements
System.out.println("=========");
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
System.out.println(sum);
// Find the largest element
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (max < array[i]){
max = array[i];
}
}
System.out.println(max);
}
}
Sparse array
package com.sxl.array;
public class Demo05 {
public static void main(String[] args) {
int[][] array = new int[11][11];
array[1][2] = 1;
array[2][3] = 2;
// Output original array
for (int[] a: array) {
for (int b: a) {
System.out.print(b+"\t");
}
System.out.println();
}
// Gets the number of valid values
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++){
if (array[i][j]!=0){
sum++;
}
}
}
System.out.println(" The number of valid values is: " +sum);
// Create 1 Sparse array No. 1 1 Row deposit: Number of rows Number of columns Number of effective numbers
int[][] array2 = new int[sum+1][3];
array2[0][0] = 11; // Number of rows
array2[0][1] = 11; // Number of columns
array2[0][2] = sum; // Number of effective numbers
// Traverse primitive 2 Dimensional array, saving non-zero arrays in sparse arrays
// Join Java Develop and exchange samples: 5931423281 Blowing water and chatting
int count = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0;j < array[i].length; j++){
if (array[i][j]!=0){
count++;
array2[count][0] = i;
array2[count][1] = j;
array2[count][2] = array[i][j];
}
}
}
System.out.println(" Output sparse array ");
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[i].length; j++){
System.out.print(array2[i][j]+"\t");
}
System.out.println();
}
}
}
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!