Length specification is required
int[] a; //Do not initialize
int[] a = new int[5]; //Initialize with 0
int[][] a = new int[2][3]; //Two-dimensional array
When not initialized, the initial value is 0
int[] a = {1, 2, 3}; //Length 3
int[][] a = {{1,2},{3,4}}; //Two-dimensional initialization
f(new int[]{1, 2, 3}); //By as a function argument@saka1029
--Attribute. Not a function. --The length cannot be changed.
a.length
a[0].length //In the case of two dimensions
--Subscript 0 Beginning ――Maximum subscript is length ―― 1 --If it exceeds, a "java.lang.ArrayIndexOutOfBoundsException" exception will be thrown. --java.util.Arrays.fill (array, value) Fill the entire array with the specified value --java.util.Arrays.fill (array, start position, completion position, value) Fill a part of the array with the specified value.
a[n];
a[i][j]; //In the case of two dimensions
--Arrays cannot be added or deleted directly. Please refer to the copy instead.
--Array.clone () -[System.arraycopy (original array, original start position, new array, new start position, length)](https://docs.oracle.com/javase/jp/8/docs/api/java/lang/System .html) ★ My recommendation --java.util.Arrays.copyOf (original array, length) --java.util.Arrays.copyOfRange (original array, start position, end position)
import java.util.Arrays;
class Rec{
String str;
public Rec(String s){str=s;}
public String toString(){return str;}
}
public class Main
{
public static void main(String[] args) {
int[] a = {1,2,3};
int[] b = a.clone();
a[1] = 7;
System.out.println("a=" + Arrays.toString(a) + " b=" + Arrays.toString(b));
String[] c = {"a", "b", "c"};
String[] d = c.clone();
c[1] = "7";
System.out.println("c=" + Arrays.toString(c) + " d=" + Arrays.toString(d));
Rec[] e = {new Rec("e"), new Rec("f"), new Rec("g")};
Rec[] f = e.clone();
Rec[] g = new Rec[3];
Rec[] h = Arrays.copyOf(e, 5);
Rec[] i = Arrays.copyOfRange(e, 2, 4);
System.arraycopy(e, 0, g, 0, g.length);
e[0] = new Rec("7");
e[1].str ="8";
g[2].str = "9";
System.out.println("e=" + Arrays.toString(e) + " f=" + Arrays.toString(f)
+ " g=" + Arrays.toString(g) + " h=" + Arrays.toString(h) + " i=" + Arrays.toString(i));
}
}
a=[1, 7, 3] b=[1, 2, 3] <== b[1]Remains 2
c=[a, 7, c] d=[a, b, c] <== d[1]Remains b
e=[7, 8, 9] f=[e, 8, 9] g=[e, 8, 9] h=[e, 8, 9, null, null] i=[9, null] <== f[0]Remains e, f[1], g[1]Changes, e[2],f[2],g[2]Will change
--Each method does not copy Shallow, that is, the contents of Object. --Clone (), copyOf (), copyOfRange () do not require the trouble of new copy destination. --clone () creates the same array as the original --copyOf () has a variable copy destination length. If it becomes shorter, it will be disconnected, and if it becomes longer, it will be complemented with 0, false, null, etc. --copyOfRange () has variable copy start and end positions (even outside the original array). 4/30 postscript: The start position position cannot be minus, that is, it cannot be added before, which is inconvenient. --arraycopy () takes time to prepare the copy destination by yourself, but it is completely free --My memo: Although arraycopy () has few restrictions, its convenience is slightly impaired. Let's go with this one rather than remembering various things.
--System.arraycopy () Reference
for(int i=0; i<a.length; i++){
// do sth. here
}
--java.util.Arrays.toString () // Expands the array --java.util.Arrays.deepToString () // Expands even multidimensional arrays
import java.util.Arrays;
int[] a = {1,2,3};
System.out.prontln("a=" + Arrays.toString(a));
int[][][] j = {{{1,2},{3,4},{5,6}},{ {7,8},{9,10},{11,12}}};
System.out.println("j=" + j + " aj=" + Arrays.toString(j) + " deepJ=" + Arrays.deepToString(j));
output
a=[1, 2, 3]
j=[[[I@2a139a55 aj=[[[I@15db9742, [[I@6d06d69c] deepJ=[[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]
sort
--java.util.Arrays.binarySearch () Binary search, sort () required in advance --My memo: Consider Sorted Map
--convert to java.util.Arrays.asList (array) List
Features not introduced above
Recommended Posts