ํ๋ก๊ทธ๋๋ฐ ๊ณต๋ถ/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));
}