Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- aws saa ํฉ๊ฒฉ
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- Entity
- s3 ์ด๋ฏธ์ง ์ ์ฅ
- docker-compose kafka
- docker
- redis ์กฐํ
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- docker ps -a
- ๋ค์ค ์ปจํ ์ด๋
- ํ๋ก๊ทธ๋๋จธ์ค
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- s3 log ์ ์ฅ
- JPA
- ์๋ฐ
- ์คํํ๋ ๋ฏธ์ค
- ํ์ดํผ๋ฐ์ด์
- private subnet ec2 ๋ก์ปฌ ์ ์
- AWS Certified Solutions Architect - Associate
- redis ํ ์คํธ์ฝ๋
- nGrinder
- Kafka
- Codedeploy ์ค๋ฅ
- docker compose
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- aws ์ฟ ํฐ
- prod docker-compose
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
Archives
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[Java] ๋ฐฐ์ด ๋ณธ๋ฌธ
๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป/๐๐๐ฏ๐
[Java] ๋ฐฐ์ด
๐คRyusun๐ค 2022. 9. 30. 16:41โญ ๋ฐฐ์ด
- ์ผ์ฐจ์ ๋ฐฐ์ด
- ๋ณ์๋ ํ ๊ฐ์ ๋ฐ์ดํฐ๋ง ์ ์ฅํ ์ ์์
- ๋์ผํ ๋ฐ์ดํฐ ํ์
์ ์ฌ๋ฌ ๊ฐ ์ ์ฅํ๊ณ ์ถ์ ๊ฒฝ์ฐ ๋ฐฐ์ด ์ฌ์ฉ
- ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์
- ๋ฐฐ์ด ์์ ์ฌ์ด์๋ ์์๊ฐ ์กด์ฌ
- ํ๋์ ๋ฐฐ์ด ์ด๋ฆ์ ๊ณต์ ํ ์ ์๊ธฐ ๋๋ฌธ์ ๋์ค์ for๋ฌธ๊ณผ ๊ฐ์ด ์ฌ์ฉ
- ์ ์ธ
- ๋ฐ์ดํฐํ์ [] ๋ฐฐ์ด๋ณ์์ด๋ฆ = new ๋ฐ์ดํฐํ์ [๋ฐ์ดํฐ ๊ฐ์];
- ๋ฐฐ์ด ์ ์ธ์ ์์๊ฐ์ ์๊ณ ์๋ ๊ฒฝ์ฐ
- ๋ฐ์ดํฐํ์ [] ๋ฐฐ์ด๋ณ์์ด๋ฆ = {..., ..., ..., ..., ...};
- ๋ฐฐ์ด ๊ฐ์ฒด๊ฐ ์๋ค๋ฉด null ์ด๊ธฐํ ๊ฐ๋ฅ
- ๋ฐ์ดํฐํ์ [] ๋ฐฐ์ด๋ณ์์ด๋ฆ = null;
int[] scores = new int[100];
for (int i = 0; i<100; i++){
scores[i] = i+1; //1๋ถํฐ ๊ฐ์ ๋ฃ์ด์ค๋ค.
int[] scores = {90, 83, 77, 100, ..., 60};
- ์ด์ฐจ์ ๋ฐฐ์ด
- ํ๊ณผ ์ด์ ๊ฐ์ง ํ ์ด๋ธ ํํ์ ๋ฐ์ดํฐ ์ ์ฅํ ๊ฒฝ์ฐ ์ด์ฐจ์ ๋ฐฐ์ด ์ฌ์ฉ
- ์ ์ธ ๋ฐฉ๋ฒ
- ๋ฐฐ์ด ์ ์ธ์ ์ ์ฅํ ํ๊ณผ ์ด์ ๊ฐ์๋ง ์๊ณ ์๋ ๊ฒฝ์ฐ
- ๋ฐ์ดํฐํ์ [][] ๋ฐฐ์ด๋ณ์์ด๋ฆ = new ๋ฐ์ดํฐํ์ [ํ์ ํฌ๊ธฐ][์ด์ ํฌ๊ธฐ];
int[][] scores = new int[1][3];
scores[0][0] = 10;
scores[0][1] = 20;
scores[0][2] = 30;
- ์ผ์ฐจ์๋ฐฐ์ด ↔ ์ด์ฐจ์๋ฐฐ์ด
public class Array1dtoArray2dExample {
public static void main(String[] args) {
final int ROW_NUM = 3;
final int COL_NUM = 5;
final int NUM = ROW_NUM * COL_NUM;
int[] arr1d = new int[NUM];
for (int i = 0; i < arr1d.length; i++) {
arr1d[i] = i + 1;
}
for (int element: arr1d) {
System.out.printf("%2d ", element);
}
System.out.println("\n");
int[][] arr2d = new int[ROW_NUM][COL_NUM];
for (int i = 0; i < arr2d.length; i++) {
for (int j = 0; j < arr2d[i].length; j++) {
arr2d[i][j] = arr1d[i * COL_NUM + j];
}
}
for (int i = 0; i < arr2d.length; i++) {
for (int j = 0; j < arr2d[i].length; j++) {
System.out.printf("%2d ", arr2d[i][j]);
}
System.out.println();
}
System.out.println("\n");
int[] arr1dRevert = new int[ROW_NUM * COL_NUM];
for (int i = 0; i < arr2d.length; i++) {
for (int j = 0; j < arr2d[i].length; j++) {
arr1dRevert[i * COL_NUM + j] = arr2d[i][j];
}
}
for (int element: arr1dRevert) {
System.out.printf("%2d ", element);
}
}
}
- ๋ฐฐ์ด ์ถ๋ ฅ
- 1์ฐจ์ ๋ฐฐ์ด : toString ํจ์ ์ฌ์ฉํ๋ค.
- ๋ค์ฐจ์ ๋ฐฐ์ด : deepToString ํจ์ ์ฌ์ฉํ๋ค.
import java.util.Arrays;
public class ArraysToStringExample {
public static void main(String[] args) {
// ์ผ์ฐจ์ ๋ฐฐ์ด ์ถ๋ ฅ
int[] arr1d = {1, 2, 3, 4, 5};
String[] strArr1d = {"Java", "JSP", "Servlet", "Spring"};
System.out.println(Arrays.toString(arr1d));
System.out.println(Arrays.toString(strArr1d));
Arrays.stream(arr1d).forEach((i) -> System.out.print(i + " ")); //stream ์ด์ฉํด์ ๋๋ค์ ํ์ฉ
Arrays.stream(strArr1d).forEach((i) -> System.out.print(i + " "));
System.out.println("\n");
// ์ด์ฐจ์ ๋ฐฐ์ด ์ถ๋ ฅ
int[][] arr2d = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
String[][] strArr2d = {{"a", "ab", "abc"}, {"1", "12", "123"}, {"a1", "b2", "c3"}};
System.out.println(Arrays.deepToString(arr2d)); //deepToString ํจ์ ์ฌ์ฉ
System.out.println(Arrays.deepToString(strArr2d));
Arrays.stream(arr2d).forEach((i) -> {
Arrays.stream(i).forEach((j) -> System.out.print(j + " "));
System.out.println();
});
Arrays.stream(strArr2d).forEach((i) -> {
Arrays.stream(i).forEach((j) -> System.out.print(j + " "));
System.out.println();
});
}
}
- ๋ฐฐ์ด ๋น๊ต
- Equals ํจ์ ์ฌ์ฉ
import java.util.Arrays;
public class ArraysEqualsExample {
public static void main(String[] args) {
// ์ผ์ฐจ์ ๋ฐฐ์ด ๋น๊ต
int[] arr1d = {1, 2, 3, 4, 5, 6};
int[] arr1dCompare = {1, 2, 3, 4, 5, 6, 7};
String[] strArr1d = {"Java", "JSP", "Servlet", "Spring"};
String[] strArr1dCompare = {"Java", "JSP", "Servlet", "Spring"};
System.out.println(arr1d == arr1dCompare); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(arr1d.equals(arr1dCompare)); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(strArr1d == strArr1dCompare); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(strArr1d.equals(strArr1dCompare)); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(isEquals(arr1d, arr1dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(isEquals(strArr1d, strArr1dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(Arrays.equals(arr1d, arr1dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(Arrays.equals(strArr1d, strArr1dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println();
// ์ด์ฐจ์ ๋ฐฐ์ด ๋น๊ต
int[][] arr2d = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] arr2dCompare = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
String[][] strArr2d = {{"a", "ab", "abc"}, {"1", "12", "123"}, {"a1", "b2", "c3"}};
String[][] strArr2dCompare = {{"a", "ab", "abc"}, {"1", "12", "123"}, {"a1", "b2", "c3"}};
System.out.println(arr2d == arr2dCompare); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(strArr2d == strArr2dCompare); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(arr2d.equals(arr2dCompare)); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(strArr2d == strArr2dCompare); // ์ฃผ์๊ฐ ๋น๊ต
for (int i = 0; i < arr2d.length; i++) {
System.out.println(arr2d[i] == arr2dCompare[i]); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(strArr2d[i] == strArr2dCompare[i]); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(arr2d[i].equals(arr2dCompare[i])); // ์ฃผ์๊ฐ ๋น๊ต
System.out.println(strArr2d[i] != null && strArr2d[i].equals(strArr2dCompare[i])); // ์ฃผ์๊ฐ ๋น๊ต
}
System.out.println(isDeepEquals(arr2d, arr2dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(isDeepEquals(strArr2d, strArr2dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(Arrays.deepEquals(arr2d, arr2dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(Arrays.deepEquals(strArr2d, strArr2dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println();
}
public static boolean isEquals(int[] a, int[] b) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
public static boolean isEquals(String[] a, String[] b) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++) {
if (a[i] != null && !a[i].equals(b[i])) return false;
}
return true;
}
public static boolean isDeepEquals(int[][] a, int[][] b) {
if (a.length != b.length) return false;
if (a[0].length != b[0].length) return false;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
if (a[i][j] != b[i][j]) return false;
}
}
return true;
}
public static boolean isDeepEquals(String[][] a, String[][] b) {
if (a.length != b.length) return false;
if (a[0].length != b[0].length) return false;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
if (a[i][j] != null && !a[i][j].equals(b[i][j])) return false;
}
}
return true;
}
}
- Compare ํจ์ ์ฌ์ฉ
import java.util.Arrays;
public class ArraysCompareExample {
public static void main(String[] args) {
int[] arr1d = {1, 2, 3, 4, 5, 6};
int[] arr1dCompare = {1, 2, 3, 4, 5, 6, 7};
String[] strArr1d = {"Java", "JSP", "Servlet", "Spring"};
String[] strArr1dCompare = {"Javp", "JSP", "Servlet", "Spring", "Python"};
System.out.println(compare(arr1d, arr1dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(Arrays.compare(arr1d, arr1dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(compare(strArr1d, strArr1dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println(Arrays.compare(strArr1d, strArr1dCompare)); // ์์๊ฐ ๋น๊ต
System.out.println();
}
public static int compare(int[] a, int[] b) {
int minLength = Math.min(a.length, b.length);
for (int i = 0; i < minLength; i++) {
if (a[i] != b[i]) {
return Integer.compare(a[i], b[i]);
}
}
if (a.length < b.length) return -1;
else if (a.length > b.length) return 1;
else return 0;
}
public static int compare(String[] a, String[] b) {
int minLength = Math.min(a.length, b.length);
for (int i = 0; i < minLength; i++) {
if (a[i] != null && !a[i].equals(b[i])) {
return a[i].compareTo(b[i]);
}
}
if (a.length < b.length) return -1;
else if (a.length > b.length) return 1;
else return 0;
}
}
- ๋ฐฐ์ด ๋ณต์ฌ
- ๋ฐฐ์ด ์ด๋ฆ์ด ์๋ ๋ฐฐ์ด ์์๋ฅผ ๋ฐ๋ณตํ๋ฉด์ ๋ณต์ฌํด์ผํจ
- Shallow Copy
- "์ฃผ์๊ฐ"์ ๋ณต์ฌ
- ์ผ์ฐจ์ ๋ฐฐ์ด ๋ณต์ฌํ ๊ฒฝ์ฐ
for (int i = 0; i < original.length(); i++) {
copy[i] = original[i];
}
-
- Deep Copy
- ์ค์ ๊ฐ'์ ์๋ก์ด ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋ณต์ฌ
- ์ด์ค ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ ๊ฒฝ์ฐ
- Deep Copy
import java.util.Arrays;
public class ArraysCopyExample {
public static void main(String[] args) {
// ์ผ์ฐจ์ ๋ฐฐ์ด ๋ณต์ฌ
int[] arr1d = {1, 2, 3, 4, 5, 6};
int[] arr1dCopy = arr1d; // ์ฃผ์๊ฐ ๋์
String[] strArr1d = {"Java", "JSP", "Servlet", "Spring"};
String[] strArr1dCopy = strArr1d; // ์ฃผ์๊ฐ ๋์
final int NEW_LEN = 7;
arr1dCopy = Arrays.copyOf(arr1d, NEW_LEN); // ๋ฐฐ์ด ์์๊ฐ ๋ณต์ฌ
strArr1dCopy = Arrays.copyOf(strArr1d, NEW_LEN); // ๋ฐฐ์ด ์์๊ฐ ๋ณต์ฌ
System.out.println(Arrays.toString(arr1dCopy));
System.out.println(Arrays.toString(strArr1dCopy));
System.out.println(arr1d == arr1dCopy); // false
System.out.println(Arrays.equals(arr1d, arr1dCopy)); // false
System.out.println(strArr1d == strArr1dCopy); // false
System.out.println(Arrays.equals(strArr1d, strArr1dCopy)); // false
arr1dCopy = copy(arr1d, NEW_LEN); // ๋ฐฐ์ด ์์๊ฐ ๋ณต์ฌ
strArr1dCopy = copy(strArr1dCopy, NEW_LEN); // ๋ฐฐ์ด ์์๊ฐ ๋ณต์ฌ
System.out.println(Arrays.toString(arr1dCopy));
System.out.println(Arrays.toString(strArr1dCopy));
// Arrays.copyOfRange() // ๋ฐฐ์ด ์ผ๋ถ ์์๋ง ๋ณต์ฌ
System.out.println(arr1d == arr1dCopy); // false
System.out.println(Arrays.equals(arr1d, arr1dCopy)); // false
// String.equals()
// for(int i = 0; i < str.length() ...) ...
System.out.println(strArr1d == strArr1dCopy); // false
System.out.println(Arrays.equals(strArr1d, strArr1dCopy)); // false
// ์ด์ฐจ์ ๋ฐฐ์ด ๋ณต์ฌ
int[][] arr2d = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] arr2dCopy = arr2d; // ์ฃผ์๊ฐ ๋์
String[][] strArr2d = {{"a", "ab", "abc"}, {"1", "12", "123"}, {"a1", "b2", "c3"}};
String[][] strArr2dCopy = strArr2d; // ์ฃผ์๊ฐ ๋์
final int NEW_ROW = 3;
final int NEW_COL = 5;
arr2dCopy = deepCopy(arr2d, NEW_ROW, NEW_COL);
strArr2dCopy = deepCopy(strArr2d, NEW_ROW, NEW_COL);
System.out.println(Arrays.deepToString(arr2dCopy));
System.out.println(Arrays.deepToString(strArr2dCopy));
System.out.println(arr2d == arr2dCopy); // false
System.out.println(Arrays.deepEquals(arr2d, arr2dCopy)); // false
System.out.println(strArr2d == strArr2dCopy); // false
System.out.println(Arrays.deepEquals(strArr2d, strArr2dCopy)); // false
arr2dCopy = deepCopyWithStream(arr2d);
strArr2dCopy = deepCopyWithStream(strArr2d);
System.out.println(Arrays.deepToString(arr2dCopy));
System.out.println(Arrays.deepToString(strArr2dCopy));
System.out.println(arr2d == arr2dCopy); // false
System.out.println(Arrays.deepEquals(arr2d, arr2dCopy)); // true
System.out.println(strArr2d == strArr2dCopy); // false
System.out.println(Arrays.deepEquals(strArr2d, strArr2dCopy)); // true
}
public static int[] copy(int[] original, int newLength) {
int[] copied = new int[newLength];
for (int i = 0; i < original.length; i++) {
copied[i] = original[i];
}
return copied;
}
public static String[] copy(String[] original, int newLength) {
String[] copied = new String[newLength];
for (int i = 0; i < original.length; i++) {
if (original[i] != null) {
copied[i] = original[i];
}
}
return copied;
}
public static int[][] deepCopy(int[][] original, int newRow, int newColumn) {
int[][] copied = new int[newRow][newColumn];
for (int i = 0; i < original.length; i++) {
for (int j = 0; j < original[i].length; j++) {
copied[i][j] = original[i][j];
}
}
return copied;
}
public static String[][] deepCopy(String[][] original, int newRow, int newColumn) {
String[][] copied = new String[newRow][newColumn];
for (int i = 0; i < original.length; i++) {
for (int j = 0; j < original[i].length; j++) {
if (original[i][j] != null) {
copied[i][j] = original[i][j];
}
}
}
return copied;
}
public static int[][] deepCopyWithStream(int[][] original) {
return Arrays.stream(original).map(r -> Arrays.copyOf(r, r.length)).toArray(int[][]::new);
}
public static String[][] deepCopyWithStream(String[][] original) {
return Arrays.stream(original).map(r -> Arrays.copyOf(r, r.length)).toArray(String[][]::new);
}
}
'๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป > ๐๐๐ฏ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์บก์ํ (0) | 2022.10.07 |
---|---|
[Java] ๊ฐ์ฒด, ์์ฑ์, ํด๋์ค (0) | 2022.10.07 |
[Java] ๋ฌธ์์ด (0) | 2022.09.30 |
[Java] ์กฐ๊ฑด๋ฌธ (0) | 2022.09.30 |
[Java] ์ ์ถ๋ ฅ, ์๋ฃํ, ์ฐ์ฐ์ (4) | 2022.09.23 |