java array

Declaration

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

Initialization

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

length

--Attribute. Not a function. --The length cannot be changed.

a.length
a[0].length //In the case of two dimensions

Substitution

--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

Add delete

--Arrays cannot be added or deleted directly. Please refer to the copy instead.

copy

--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)

Let's look at an example of clone () and arraycopy ()
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));
	}
}
Execution result
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
Differences between various copies

--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.

Partially taken out

--System.arraycopy () Reference

loop

for(int i=0; i<a.length; i++){
  // do sth. here
}

Stringification

--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

Search

--java.util.Arrays.binarySearch () Binary search, sort () required in advance --My memo: Consider Sorted Map

Type conversion

--convert to java.util.Arrays.asList (array) List

Related Class

Features not introduced above

reference

  1. https://www.sejuku.net/blog/14461

Recommended Posts

[Java] array
Java array
java (array)
Java array
[Java] Array
Java array
java array
[Java] Array
java array variable
[Java] Array notes
Array
About Java Array List
[Java ~ Array ~] Study memo 4
Java
Array
[Java] List type / Array type conversion
Java Development Basics ~ Exercise (Array) ~
[Java] Convert ArrayList to array
[Java Silver] Array generation method
How to initialize Java array
[Beginner] Java basic "array" description
Java learning (0)
Studying Java ―― 3
Java protected
[Java] Annotation
[Ruby] Array
Studying Java ―― 9
Java scratch scratch
Array practice
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
Java learning memo (creating an array)
Java static
java beginner 4
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
Java memorandum
☾ Java / Collection
Studying Java ―― 1
[Java] Polymorphism
Studying Java # 0
Java review
java framework
[Java] Inheritance
FastScanner Java
java beginner 3
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Array practice 2
[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)
Decompile Java