arrays are contiguous spaces that store the same data types in memorydeclaring an array is a sequence of contiguous spaces in the memory space.The array name represents the first address of the contiguous space through the first address you can access the array all element elements are sorted in the array called Subscript zero-basedUse of Arraysonce the array length is declared, cannot be changed and cannot be appendedDeclares an array of type int int[] arr; or int arr[] allocates space to an array arr=new int[5]; Assigning a value to an array arr[0]=1; 0 represents the 1th element of the array, and the element is labeled 0 arr[1]=1; 1 represents the 2nd element of the array, the element is labeled 1 to access the array data, and the array subscript is used to access the c=arr[4]; Array declaration abbreviation int[] Arr={12,3,4,8,5,6,6,7,8,8,9,8};int [] arr1=new int[]{12,3,4,8,5,6,6,4};new int[] [] must not put memory size using loops to assign values to an array Example is int[] arr=new int[5];
for (int i = 0; i < arr.length; i++) {
SYSTEM.OUT.PRINTLN ("array" + (i+1) + "value of" +i);
} using loop traversal to assign values to the array, the output array instance is int[] arr={1,3,6,5,6,7,84,55,5};
for (int num:arr) {
SYSTEM.OUT.PRINTLN (num);
} Displays the result as
Two methods that are commonly used to sort in arrays
Bubble SortExample: Public static void main (String[] args) { achieving student performance ranking and showing student performance list int [ ] arr = {78,89,67,98,90,56,88};①n number of arrays, need to compare N-1 round for (int i=0; i<arr.length; i++) {for (int j=i; j<arr.length; j++) { if (Arr[i]>arr[j]) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } }}② Bubble Sort Ascending order for (int i=0; i<arr.length-1; i++) { for (int j=0; j<arr.length-1-i; j++) { if (Arr[j]>arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } }
Sorting by dichotomy Find int start = 0; by dichotomy // default Start coordinate int& nbsp;end = arr.length-1; // default end coordinates int index = -1; // cannot find the default index of-1 while (start<=end) { & nbsp; int middle = (start+end)/2; // calculate intermediate subscript if (Num == arr[middle]) { index = middle; break; } if (num > arr[middle]) { start = middle + 1; } if (Num < arr[middle]) { end = middle - 1; }} System.out.println (index); }}
Here are some examples. ① make a shopping list, enter the amount manually, the output is to show the total amount Scanner in=new Scanner (system.in);
Double sum=0;
Double[] Arr=new double[5];
for (int i = 0; i < arr.length; i++) {
System.out.print ("Please enter" + (i+1) + "Purchase amount:");
Arr[i]=in.nextdouble ();
Sum+=arr[i];
}
SYSTEM.OUT.PRINTLN ("Serial number \t\t amount");
for (int i = 0; i < arr.length; i++) {
System.out.println ((i+1) + "\t\t" +arr[i]);
}
SYSTEM.OUT.PRINTLN ("Total amount:" +sum);
} Displays the result as
② Enter the price of four stores and find out the minimum price
Scanner in=new Scanner (system.in);
Int[] Arr=new int[4];
System.out.println ("Please enter the price of four stores");
for (int i = 0; I <4; i++) {
System.out.print ("+ (i+1) +" The price of the shop is: ");
Arr[i]=in.nextint ();
}
for (int j = 0; J < Arr.length-1; J + +) {
if (Arr[j]<arr[j+1]) {
ARR[J+1]=ARR[J];
}else {
ARR[J+1]=ARR[J+1];
}
}system.out.println ("The lowest price is:" +arr[3]);
} Displays the result as
③ arranges an array from small to large
Int[] arr={56,45,99,82,91,81};
int Max;
int m;
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; J < Arr.length-1-i; J + +) {
if (Arr[j]>arr[j+1]) {
int TEMP=ARR[J];
ARR[J]=ARR[J+1];
Arr[j+1]=temp;
}
}
}
for (int a:arr) {
System.out.println (a);
}
}
} Displays the result as
④ Enter a number, look in the array, if there is an output subscript, if there is no output-1
Scanner in=new Scanner (system.in);
Int[] arr={12,34,46,68,32,35,75,48,25};
System.out.println ("Enter a number");
int Sz=in.nextint ();
for (int i = 0; i < arr.length; i++) {
if (Sz==arr[i]) {
SYSTEM.OUT.PRINTLN ("digital subscript" +i);
Return
}
}
System.out.println ("-1");
Show results as
⑤ writes a Java program, implements merging of two arrays, and arranges the merged array in ascending order
Assume that there are two existing arrays, namely:
Int[] arr1={3, 1, 23};
Int[] arr2={27, 7, 2};
The design program merges two arrays into an array and arranges the merged array in ascending order, outputting the pre-merged and merged array information
Int[] arr1 = {3, 1, 23};
Int[] arr2 = {27, 7, 2};
int[] arr = new Int[arr1.length+arr2.length]; 1. Declares an array that is equal to the length of two arrays and
for (int i=0; i<arr1.length; i++) {//2. Traversal of two arrays
Arr[i] = Arr1[i];
}
for (int i=0; i<arr2.length; i++) {
Arr[arr1.length+i] = Arr2[i];
}
for (int i=0; I <arr.length-1; i++) {//3. Sorting the new array
for (int j=0; J <arr.length-1-i; J + +) {
if (arr [j]>arr[J+1]) {
int temp = arr[J];
arr [j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println (arrays.tostring (arr));
Show results as
⑥ use an array to determine whether a number is a palindrome.
System.out.print ("Please enter an array:");
Scanner in = new Scanner (system.in);
Long num = In.nextlong ();
long old = num; Scratch num
int count = 0; Num has several, defining a few arrays. Find out the number of digits in num
while (num>0) {
Num/= 10;
count++;
}
int[] arr = new Int[count];
Count = 0; Constantly take out every bit of the number and put it in the array
while (old>0) {
Arr[count] = (int) Old% 10;
Old/= 10;
Count + +;
}
for (int i=0,j=arr.length-1; i<=j; i++,j--) {//compares the end and end of the array in turn. Any one is not equal, it is not a palindrome number
if (arr[i]! = Arr[j]) {
System.out.println ("not palindrome number");
Return
}
}
System.out.println ("is a palindrome number");
Show results as
⑦ input array, largest interchange with first element, smallest swap with last element, output array
Scanner in=new Scanner (system.in); Set input Array
int [] arr;
SYSTEM.OUT.PRINTLN ("input array");
for (int i = 0; i < arr.length; i++) {
int Arr[i]=in.nextint ();
int [] arr = {5,3,7,1,0,6,9,2};
int max = Arr[0], Maxindex = 0; Find the index of the largest element
for (int i=0; i<arr.length; i++) {
if (Max < arr[i]) {
max = Arr[i];
Maxindex = i;
}
}
if (Maxindex! = 0) {
int temp = arr[0];
Arr[0] = Arr[maxindex];
Arr[maxindex] = temp;
}
int min = arr[0], Minindex = 0; Find the subscript for the smallest element
for (int i=0; i<arr.length; i++) {
if (min > Arr[i]) {
min = Arr[i];
Minindex = i;
}
}
if (minindex! = (arr.length-1)) {
int temp = arr[arr.length-1];
ARR[ARR.LENGTH-1] = Arr[minindex];
Arr[minindex] = temp;
}
System.out.println (arrays.tostring (arr));
}
Show results as
Arrays in Java