How to initialize an array
package Arrays; import java.util.*; class Person{} public class ArrayOptions { public static void main(String[] args) { /** * object array */ Person[] p1;//Local variables without initialization are not allowed before initialization. //1, Person[] p2=new Person[2];//null in object array System.out.println("p2:"+Arrays.toString(p2)); //p2:[null, null] //2, Person[] p3=new Person[2]; for(int i=0;i<p3.length;i++){ p3[i]=new Person();//Loop objects into an array } //3, Person[] p4={new Person(),new Person(),new Person()}; //4. Declared before and initialized later. p1=new Person[]{new Person(),new Person()}; System.out.println("P1:"+Arrays.toString(p1));//P1:[Arrays.Person@15db9742, Arrays.Person@6d06d69c] /** * Array of basic types */ int[] a; int[] b=new int[2]; System.out.println("b:"+Arrays.toString(b));//b:[0, 0] int[] c=new int[2]; for(int i=0;i<c.length;i++){ c[i]=i; } System.out.println("c:"+Arrays.toString(c));//c:[0, 1] int[] d={2,3,1}; System.out.println("d:"+Arrays.toString(d));//d:[2, 3, 1] a=new int[]{3,5,6}; System.out.println("a:"+Arrays.toString(a));//a:[3, 5, 6] char[] f=new char[2]; System.out.println("f:"+Arrays.toString(f)); System.out.println(Arrays.toString(returnArray()));//[joaid, ajoid] } /** * Returns an array * @return */ public static String[] returnArray(){ String[] c=new String[]{"joaid","ajoid"}; return c; } }
The object array holds the reference of the object, and the basic type array holds the value.
The use of object array and basic type array is basically the same
Replicated array
1. System.arraycopy (source array, starting from where the source array is copied, target array, starting from where the target array is copied, the number of elements to be copied)
//Basic types int[] g=new int[]{1,2,3}; int[] h=new int[]{4,5,6,8,9}; System.arraycopy(g,0,h,0,g.length); System.out.println("g:"+Arrays.toString(g));//g:[1, 2, 3] System.out.println("h:"+Arrays.toString(h));//h:[1, 2, 3, 8, 9] g[1]=5;//change System.out.println("g:"+Arrays.toString(g));//g:[1, 5, 3] System.out.println("h:"+Arrays.toString(h));//h:[1, 2, 3, 8, 9] //Reference type (both point to an address and both change) Person[] p5=new Person[]{new Person(),new Person(),new Person()}; Person[] p6=new Person[5]; System.arraycopy(p5,0,p6,0,p5.length); System.out.println("p5"+Arrays.toString(p5));//p5[id=2, id=2, id=2] System.out.println("p6"+Arrays.toString(p6));//p6[id=2, id=2, id=2, null, null] p5[1].setId(5); System.out.println("p5"+Arrays.toString(p5));//p5[id=2, id=5, id=2] System.out.println("p6"+Arrays.toString(p6));//p6[id=2, id=5, id=2, null, null]
2,clone()
int[] i=new int[]{1,2,3,2,1}; int[] j=i.clone(); i[1]=5;//change System.out.println("i"+Arrays.toString(i));//i[1, 5, 3, 2, 1] System.out.println("j"+Arrays.toString(j));//j[1, 2, 3, 2, 1] does not change
3. Arrays.copyof (original array, new array length);
int[] k=new int[]{2,5,3,6,9}; int[] l; l=Arrays.copyOf(k,k.length); System.out.println("l:"+Arrays.toString(l));//l:[2, 5, 3, 6, 9] k[2]=5; System.out.println("k:"+Arrays.toString(k));//k:[2, 5, 5, 6, 9] System.out.println("l:"+Arrays.toString(l));//l:[2, 5, 3, 6, 9]
In the copy array in the above 3, in the object array, it is shallow copy. The copied array and the source array point to the same address, but copy the reference of the object, not the copy of the object itself.
Delete duplicates in sort array
For example: nums = [0,0,1,1,1,2,2,3,3,4],
Because it's an ordered array, you can use the double finger method to compare the two numbers before and after.
public int removeDuplicates(int[] nums) { //Double pointer: compares whether the previous and existing numbers are equal. int j=1;//It is used to record different numbers. 2. for(int i=1;i<nums.length;i++){ if(nums[i]!=nums[i-1]){ nums[j]=nums[i]; j++; } } return j; }