[Java]copyOfで配列のコピーを行う

2021年9月2日

書式
String[] 新配列=Arrays.copyof(旧配列,旧配列.length)

使用例

package com.arkgame.demo;

import java.util.Arrays;

public class ArrdataCopyDemo {

      private static String[] srcArr = { "study", "skill", "in", "arkgame.com" };
      private static String pt = " ";

      public static void main(String[] args) {

            System.out.println("配列のコピーBefore:");
            for (String st : srcArr) {
                  System.out.print(st + pt);
            }
            
            String[] desc = Arrays.copyOf(srcArr, srcArr.length);
            System.out.println("\n配列のコピーAfter:");
            for (String st : desc) {
                  System.out.print(st + pt);
            }
      }

}

結果
配列のコピーBefore:
study skill in arkgame.com
配列のコピーAfter:
study skill in arkgame.com

Java

Posted by arkgame