Java 配列をコピーするサンプル

環境
Windows11 home
java 19.0.1

構文
Arrays.copyOf( 配列 , サイズ )
配列をコピーするには、「Arrays.copyOf」関数を使用します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
int[] a = {31, 42, 53};
int[] b = Arrays.copyOf(a, 3);
System.out.println(a[0]);
System.out.println(b[0]);
}
}
import java.util.*; public class Main { public static void main(String[] args) throws Exception { int[] a = {31, 42, 53}; int[] b = Arrays.copyOf(a, 3); System.out.println(a[0]); System.out.println(b[0]); } }
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
            int[] a = {31, 42, 53};
        int[] b = Arrays.copyOf(a, 3);

        System.out.println(a[0]); 
        System.out.println(b[0]); 

    }
}

実行結果
31
31

IT

Posted by arkgame