Write in front:
Hello, everyone. I’m Fdog, from a small city in Inner Mongolia. I’m currently studying in Taizhou. I am very grateful to have such a platform where I can share what I have learned and felt. I like programming, I like code, I like to be a programmer. Study hard, strive for many years later, to give their relatives a better life.
@[TOC]
1. Go through the number group
// The first way is to use the for loop for traversal
package number;
public class Xuexi {
public static void main(String[] args) {
int b[][]=new int[] [] {{1}, {2.3}, {4.5.6}};
for(int k=0; k<b.length; k++) {for(int c=0;c<b[k].length;c++)
{
System.out.println(b[k][c]);
}
System.out.println();
}
}
}
Copy the code
The running result is shown as follows:! [insert picture description here] (img – blog. Csdnimg. Cn / 20200204183… Foreach =500x foreach =500x foreach =500x
package number;
public class Xuexi {
public static void main(String[] args) {
int b[][]=new int[] [] {{1}, {2.3}, {4.5.6}};
int i=0;
for(int x[]:b)
{
i++;
int j=0;
for(int e:x)
{
j++;
if(i==b.length&&j==x.length)
{
System.out.print(e);
}
else
{
System.out.print(e+",");
}
}
}
}
}
Copy the code
Running results:
I don’t know if you notice the difference between print and println, but if you look at the first piece of code, it does a line break, and the second piece of code doesn’t, so if you look up ==print+ line break == println==.
2. Populate the replacement array elements
Once you’ve defined the elements in the array, you can replace them with the static method fill () of the Arrays class.
(1) Fill (int[] a,int value) a: array to be replaced value: value to be replaced
(2) fill (int[] a,int fromIndex,int toIndex,int value) fromIndex: toIndex: toIndex: toIndex ==(excluding)==
The difference is that (1) is used for all substitutions and (2) is used to specify substitutions.
package number;
import java.util.Arrays;Don't forget to add the Attays class
public class Xuexi {
public static void main(String[] args) {
int arr1[]=new int[] {1.2.3.4.5};
Arrays.fill(arr1,8);
for(int i=0; i<arr1.length; i++) { System.out.println("After all the substitutions."+arr1[i]);
}
System.out.println();
int arr2[]=new int[] {1.2.3.4.5};
Arrays.fill(arr2,2.4.1);
for(int i=0; i<arr2.length; i++) { System.out.println("After specified replacement"+arr2[i]); }}}Copy the code
Running results:
3. Sort the array
You can use the sort() method to sort
package number;
import java.util.Arrays;
public class Xuexi {
public static void main(String[] args) {
int arr[]=new int[] {11.27.13.48.45};
Arrays.sort(arr);
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
Copy the code
Running results:
4. Copy the array
The copyOf () and copyOfRange () methods of the Arrays class copy Arrays to a specified length, and the copyOfRange () method copies a specified length of a specified array to a new array.
CopyOf (arr, newLength) arr: array to copy newLength: If the length of the new array is greater than the length of arR, fill it with 0. If the length of the new array is smaller than the length of ARR, the array will be truncated from the first element of arR until the length of the new array is satisfied.
package number;
import java.util.Arrays;
public class Xuexi {
public static void main(String[] args) {
int arr[]=new int[] {11.27.13.48.45};
int newarr[]=Arrays.copyOf(arr,5);
for(int i=0;i<newarr.length;i++)
{
System.out.println(newarr[i]);
}
}
}
Copy the code
Running results:
CopyOfRange (arr,int formIndex,toIndex) formIndex: Specifies the index position to start copying the array. It must be between 0 and the entire array length. ToIndex: The last index position of the range to be copied, which can be greater than the length of arR, but does not include elements of the toIndex index.
package number;
import java.util.Arrays;
public class Xuexi {
public static void main(String[] args) {
int arr[]=new int[] {11.27.13.48.45};
int newarr[]=Arrays.copyOfRange(arr,0.3);
for(int i=0;i<newarr.length;i++)
{
System.out.println(newarr[i]);
}
}
}
Copy the code
Running results:
package number;
import java.util.Arrays;
public class Xuexi {
public static void main(String[] args) {
int arr[]=new int[] {11.27.13.48.45};
int newarr[]=Arrays.copyOfRange(arr,0.9); // Change 3 to 9
for(int i=0;i<newarr.length;i++)
{
System.out.println(newarr[i]);
}
}
}
Copy the code
Run result:! [insert picture description here] (img – blog. Csdnimg. Cn / 20200204195… =400x)
5. Array query
(1) binarySearch(Object[],Object key) a: the array to search for. Key: the value to search for, returns the index (subscript) if the key is in the array, or -1 or – (insertion point) otherwise.
(2) binarySearch(Object[],int formIndex,int toIndex,Object key) formIndex: index at the beginning of specified range toIndex: index at the end of specified range (not included)
The code for this method is not listed in the same way as above.
If there are mistakes, welcome to criticize, welcome to discuss. There are two most important things in life: one is to learn to choose, the other is to learn to give up. Our destiny depends on the choices we make. All choices have risks. All choices have costs. It’s about what you want and what you’re willing to give up. Giving up is not admitting defeat, is not surrender, but a more important choice. If you don’t give up, you can’t make a choice. Choice and give up, is to determine the fate of the crossroads. = =