Backend/JAVA2 멘토시리즈

JAVA Eclipse 15 배열의 복제, for ~ each문

쏠솔랄라 2023. 4. 5. 07:55

 

 

배열의 복제

 

 

System.array(src, srcPos, dest, destPos, length);

src : 복사할 배열

srcPos : 복사를 하기 시작힐 인덱스(위치)

dest : 덮어 쓸 배열

destPost : 덮어쓰기 시작할 인덱스(위치)

length : 복사할 길이

 

 

ex.

import java.util.Arrays;

public class array {

public static void main(String[] args) {

 

int [] a = {1, 2, 3, 4, 5, 6};

int [] b = {0, 0, 0, 0, 0, 0, 0, 0};

 

System.out.println(Arrays.toString(a));

System.out.println(Arrays.toString(b));

 

System.arraycopy(a, 2, b, 3, 4);

// a배열의 2위치(3 ; 배열은 0부터 시작)부터

// b배열에다가 3위치자리부터 4개를 넣는다

System.out.println(Arrays.toString(b));

 

}

}

 

출력화면

 

 


 

for ~ each문

 

: 배열 순환 시, 반복문보다 편리하게 순환할 수 있도록 

새로운 문법을 제공

 

for (자료형 변수명 : 배열명) {
변수가 배열을 순환하면서 반복할 명령;
}

 

ex1.

String a [] = {"Java", "Hello", "Programming"};

for (String i : a) {

// i라는 변수가 a라는 배열을 순환할게

System.out.println(i);

}

 

 

 

ex2.

int b[] = {1, 2, 3, 4, 5};

for (int i : b) {

System.out.println(i);

}

 

 

 

ex3.

import java.util.Scanner;

public class arrayForEach {

public static void main(String[] args) {

 

Scanner sc = new Scanner(System.in);

System.out.println("주문할 과일 3개를 입력하세요 : ");

String fruits [] = {sc.nextLine(), sc.nextLine(), sc.nextLine()};

 

System.out.println("================================");

System.out.println("주문받은 과일");

for (String eachFruits : fruits) {

System.out.println(eachFruits);

}

 

}

}

 

 

 


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93

 

JAVA Eclipse 09 제어문 : 반복문 for

https://developernew.tistory.com/102

 

JAVA Eclipse 10 제어문 : 반복문 while, do-while

https://developernew.tistory.com/103

 

JAVA Eclipse 11 제어문 : 반복문의 break, continue

https://developernew.tistory.com/106

 

JAVA Eclipse 12 배열 : 배열의 개념 및 사용

https://developernew.tistory.com/107

 

JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기

https://developernew.tistory.com/115

 

JAVA Eclipse 14 배열 : 다차원배열

https://developernew.tistory.com/117