ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€/Java

[10์ผ์ฐจ] ๋ฐฐ์—ด Array (2)

๋Œ€์žฅ์ฟต์•ผ 2023. 2. 15. 23:15

Ex34 ~ Ex35

๋ฐฐ์—ด์„ ๋Œ€์ƒ์œผ๋กœ ์ž์ฃผํ•˜๋Š” ๋ฉ”์†Œ๋“œ

 

๊ฒ€์ƒ‰

- boolean contains(๋ฐฐ์—ด, ๊ฒ€์ƒ‰๋Œ€์ƒ)

	int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	
	if (contains(nums, 15)) {
		System.out.println("๋ฐœ๊ฒฌํ•จ");
	} else {
		System.out.println("๋ฐœ๊ฒฌ๋ชปํ•จ");
	}
	
	
	String[] colors = { "red", "yellow", "blue", "white", "black", "green", "purple", "gold", "silver" };
	
	if (contains(colors, "skyblue")) {
		System.out.println("๋ฐœ๊ฒฌํ•จ");
	} else {
		System.out.println("๋ฐœ๊ฒฌ๋ชปํ•จ");
	}
	
}
	
//๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ
public static boolean contains(String[] colors, String color) {
	
	for (int i=0; i<colors.length; i++) {
		
		if (colors[i].equals(color)) {
			
			return true;
		}
		
	}
	
	return false;
}

public static boolean contains(int[] nums, int num) {
	
	for (int i=0; i<nums.length; i++) {
		
		if (nums[i] == num) {
			
			//๋ฉ”์†Œ๋“œ ์ข…๋ฃŒ + true ๋ฐ˜ํ™˜
			return true;
		}
		
	}
	
	return false;
	
}

 

๊ฒ€์ƒ‰

-int indexOf(๋ฐฐ์—ด, ๊ฒ€์ƒ‰๋Œ€์ƒ)

-๊ฒ€์ƒ‰๋Œ€์ƒ์ด ๋ฐฐ์—ด์˜ ๋ช‡๋ฒˆ์งธ ๋ฐฉ์—์„œ ๋ฐœ๊ฒฌ๋๋Š”์ง€? ๋ฐฉ๋ฒˆํ˜ธ ๋ฐ˜ํ™˜

		String[] colors = { "red", "yellow", "blue", "white", "black", "green", "purple", "gold", "silver" };
		
		System.out.println(indexOf(colors, "white"));
		System.out.println(indexOf(colors, "skyblue"));
		
	}

	public static int indexOf(String[] colors, String color) {
		
		for (int i=0; i<colors.length; i++) {
			
			if (colors[i].equals(color)) {
				
				return i; //์ฐพ์€ ๋ฐฉ๋ฒˆํ˜ธ ๋ฐ˜ํ™˜
			}
			
		}
		
		return -1; //์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค
	}

 

๊นŠ์€ ๋ณต์‚ฌ

- ๋ฐฐ์—ด deepCopy(๋ฐฐ์—ด)

		String[] colors = { "red", "yellow", "blue", "white", "black", "green", "purple", "gold", "silver" };
		
		String[] copy = deepCopy(colors);
		
		//ํ™•์ธ
		colors[0] = "๋นจ๊ฐ•";
		
		System.out.println(Arrays.toString(colors));
		System.out.println(Arrays.toString(copy));
		
	}

	public static String[] deepCopy(String[] colors) {
		
		//์›๋ณธ๊ณผ ๋™์ผํ•œ ํƒ€์ž… + ๋™์ผํ•œ ๊ธธ์ด > ๋ฐฐ์—ด ์ƒ์„ฑ
		String[] temp = new String[colors.length];
		
		for (int i=0; i<colors.length; i++) {
			
			temp[i] = colors[i];
			
		}
		
		return temp;
	}

 

์ถ”์ถœ

- ์ƒ‰์ƒ colorAt(๋ฐฐ์—ด, ๋ฐฉ๋ฒˆํ˜ธ)
- ๋ฐฐ์—ด subArray(๋ฐฐ์—ด, ์‹œ์ž‘ ๋ฐฉ๋ฒˆํ˜ธ, ๋ ๋ฐฉ๋ฒˆํ˜ธ(ํฌํ•จx)) > ๋ถ€๋ถ„ ์ง‘ํ•ฉ ๊ฐ€์ ธ์˜ค๊ธฐ
- ๋ฐฐ์—ด subArray(๋ฐฐ์—ด, ์‹œ์ž‘ ๋ฐฉ๋ฒˆํ˜ธ) > ๋๊นŒ์ง€

	private static void m19() {
		
		//- ์ถ”์ถœ
		//- ์ƒ‰์ƒ colorAt(๋ฐฐ์—ด, ๋ฐฉ๋ฒˆํ˜ธ)
		//- ๋ฐฐ์—ด subArray(๋ฐฐ์—ด, ์‹œ์ž‘ ๋ฐฉ๋ฒˆํ˜ธ, ๋ ๋ฐฉ๋ฒˆํ˜ธ(ํฌํ•จx)) > ๋ถ€๋ถ„ ์ง‘ํ•ฉ ๊ฐ€์ ธ์˜ค๊ธฐ
		//- ๋ฐฐ์—ด subArray(๋ฐฐ์—ด, ์‹œ์ž‘ ๋ฐฉ๋ฒˆํ˜ธ) > ๋๊นŒ์ง€
		
		String[] colors = { "red", "yellow", "blue", "white", "black", "green", "purple", "gold", "silver" };
		
		System.out.println(colorAt(colors, 5));
		System.out.println(colors[5]);
		
		String[] sub = subArray(colors, 2, 5);
		
		System.out.println(Arrays.toString(sub));
		
		sub = subArray(colors, 5);
		
		System.out.println(Arrays.toString(sub));
				
	}
	
	public static String[] subArray(String[] colors, int beginIndex) {
		
		//endIndex ์—†์Œ > ๋ฐฐ์—ด ๋๊นŒ์ง€(length)
		String[] temp = new String[colors.length - beginIndex]; //3
		
		for(int i=beginIndex;i<colors.length;i++) {
			
			//์ถ”์ถœํ•˜๊ณ  ์‹ถ์€ ์˜์—ญ์˜ ์ธ๋ฑ์Šค
			temp[i-beginIndex] = colors[i];
		}
		
		return temp;
	}
	
	
	public static String[] subArray(String[] colors, int beginIndex, int endIndex) {
		
		//2,5 > 2,3,4
		String[] temp = new String[endIndex - beginIndex]; //3
		
		for(int i=beginIndex;i<endIndex;i++) {
			
			//์ถ”์ถœํ•˜๊ณ  ์‹ถ์€ ์˜์—ญ์˜ ์ธ๋ฑ์Šค
			temp[i-beginIndex] = colors[i];
		}
		
		return temp;
	}
	
	public static String colorAt(String[] colors, int index) {
		
		return colors[index];
		
	}

 


์‚ฝ์ž…(์ค‘์š”***)
- ๋ฐฐ์—ด์˜ ์›ํ•˜๋Š” ์œ„์น˜์— ์š”์†Œ๋ฅผ ์‚ฝ์ž…
- Right Shift

		String[] list = { "A", "B", "C", "D", "E" };
		int index = 1; 
		String value = "F";
		
		//1, 2, 3 > 3, 2, 1
		//for (int i=index; i<list.length-1; i++) {
		for (int i=list.length-2; i>=index; i--) {
			
			System.out.println(i);
			list[i+1] = list[i];
			
		}
		
		list[index] = value;
		
		System.out.println(Arrays.toString(list));
		
	}

 

์‚ญ์ œ(์ค‘์š”***)

- ๋ฐฐ์—ด์˜ ์›ํ•˜๋Š” ์œ„์น˜์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œ
- Left Shift

		String[] list = { "A", "B", "C", "D", "E" };
		int index = 1;
		
		for (int i=index; i<list.length-1; i++) {
			//System.out.println(i);
			
			list[i] = list[i+1];
			
		}
		
		list[list.length-1] = null;
		
		System.out.println(Arrays.toString(list));
		
	}