본문 바로가기

콤퓨타/Programming) 왕초보 자바

32. (Arrays) Iterating Over Arrays

 

배열은 리스트, DB, index(?) 관리하기 위한 방법론인것 같다.

 

Array

Arrays of String

Iterating Over Arrays

 

배열은 0부터 시작하고, 

int, String 모두 수용 가능 

웹개발 처음 할때 많이 보던 내용인걸봐서, 웹에서 정보 왔다갔다할때 많이 사용하는것이 아닐까 조심스레 추측 

 

public class App {

	public static void main(String[] args) {

		int[] numbers = { 2, 4, 6, 8 };

		/*
		 * 0. Tomatoes 1. Pasta 2. Garlic 3. Onions
		 * 
		 * Note! Arrays indexes are zero-based
		 */

		System.out.println(numbers[0]);
		System.out.println(numbers[3]);
		// System.out.println(numbers [10]);

	}
}

 

0부터 시작하기 때문에, 가장 먼저 있는 숫자'2'가 출력됌. 

 

 

public class App {

	public static void main(String[] args) {
		/*
		 * 0. Potatoes 1. Rice 2. Pasta 3. Bread
		 * 
		 */

		String[] foods = { "Potatoes", "Rice", "Pasta", "Bread" };

		System.out.println(foods[0]);
		System.out.println(foods[1]);
		System.out.println(foods[2]);
		System.out.println(foods[3]);

		foods[1] = "Tomatoes";
		System.out.println(foods[1]);

	}

}

 

중간에 배열을 조정해주면, 그 조정한 곳 부터 반영되어 다른 값이 출력된다. 

food[1] 은 중간에 Rice에서 Tomatoes로 값이 변경됌

 

 

 

 

이제 for문을 이용해서 배열을 조금 더 복잡하게 만들텐ㄷ, 

public class App {
public static void main (String[]args) {
	
	String[] animals = {"cat", "dog", "sloth"}; 
	
	for(;;)
		System.out.println(animals);
}
}

선생님이 for문을 사용해서 animal 값이 하나씩 나오게 해봐 이러시길래. 

그냥 의식의 흐름대로 저렇게 써봤더니만

 

 

그냥 난리가 났다 아주그 냥...

 

 

 

적이요 선생님, for문은 기억조차 안난다고요. 

...

 

혹시 그럼 이거?

public class App {
public static void main (String[]args) {
	
	String[] animals = {"cat", "dog", "sloth"}; 
	
	for(;;)
		System.out.println(animals[0]);
	
}
}

 

 

 

캣 무한루프 오픈....

 

 

그래서 선생님 출동. 

아. for 문은 int 정해주고, int 범위 정해주고 하는거구나... 아... ㅇㅋㅇㅋ... 

 

public class App {
	public static void main(String[] args) {

		String[] animals = { "cat", "dog", "sloth" };

		for (int i = 0; i < 3; i++) {

			System.out.println(animals[i]);

		}
	}
}

 

 

혹은 조금 더 fancy 하게 할 수 있는데, 

여기에서 나오는 .length는 String method call이 아니고, 

그냥, 배열에서 값 가져오는거라고..  

 

public class App {
	public static void main(String[] args) {

		String[] animals = { "cat", "dog", "sloth", "elephant"};

		
		for (int i = 0; i <animals.length; i++) {
//.length는 method call이 아님, 그냥 geting a value from animals arrays, accessing to the property of array. 
			System.out.println(animals[i]);

		}
	}
}

 

 

 

혹은 지난번에 배운 내용처럼 포멧을 이용할 수 도 있다. 

 

public class App {
	public static void main(String[] args) {

		String[] animals = { "cat", "dog", "sloth", "elephant"};
		
		for (int i = 0; i <animals.length; i++) {
			//.length는 method call이 아님, 그냥 geting a value from animals arrays, accessing to the property of array. 
			//System.out.println(animals[i]);

			System.out.printf("%d. %s\n", i, animals[i]);
		}
	}
}

 

 

%d는 숫자 

%s는 문자열 

 

 

 


 

연습문제 1

숫자 배열 출력하기

  • numbers 배열에 5개의 정수가 들어있음
  • numbers.length를 사용해 반복 횟수 결정
  • numbers[i]로 각 숫자에 접근
public class Practice_01 {

	public static void main(String[] args) {

		int[] numbers = { 10, 20, 30, 40, 50 };
		for (int i = 0; i < numbers.length; i++) {
			System.out.printf("INDEX %d: %d\n", i, numbers[i]);
			// System.out.println(numbers[3]);
		}

	}

}

 

 

 

 

 

 

연습문제 2

  • char 타입 배열
  • %c 포맷 지정자 사용
public class Practice_02 {

	public static void main (String[]args) {
		
		char[] letters = {'A', 'B', 'C', 'D', 'E'}; 
		for (int i = 0; i < letters.length; i++) {
			System.out.printf("%d번 글자: %c\n", i, letters[i]);
		}
	}

}