λ°°μ΄(Array)
- λ³μμλ νλμ κ°λ§ λ΄μ μ μλ€
- μ¬λ¬ κ° κ°μ μ¬μ©νλ λ³μλ₯Ό λ§λ€κ³ μΆμΌλ©΄ λ°°μ΄μ μ¬μ©
λ°°μ΄ μ μΈ
λ°°μ΄μ μ μΈνλ 2κ°μ§ λ°©μ
λ³μ νμ
[ ] λ°°μ΄ μ΄λ¦ ex) int[ ] score;
λ³μ νμ
λ°°μ΄ μ΄λ¦ [ ] ex) int score[ ];
λ°°μ΄ μ΄κΈ°ν
- λ°°μ΄μ κ°λ€μ μ§μ μ λ ₯
λ³μ νμ
[ ] λ°°μ΄ μ΄λ¦ = {κ°1, κ°2...}
ex) int[ ] score = {100, 90, 80}
- new ν€μλ μ¬μ© νμ¬ μ΄κΈ°ν
- λ³μ νμ
[ ] λ°°μ΄ μ΄λ¦ = new λ³μ νμ
[ ]{κ°1, κ°2, κ°3....}
ex) int[ ] score = new int[ ] {100, 90, 80}
- new ν€μλλ₯Ό μ¬μ©νμ¬ λ°°μ΄μ κΈΈμ΄λ§ μ§μ ν κ°μ λͺ¨λ μ΄κΈ°ν
- λ³μ νμ μ λ°λΌ μ΄κΈ°νλλ κ°μ΄ λ¬λΌμ§
λ³μ νμ
[ ] λ°°μ΄ μ΄λ¦ = new λ³μ νμ
[λ°°μ΄μ κΈΈμ΄]
ex) int[ ] score = new int[3]
// μ½λ μ€μ΅
public class _01_array {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1. κ° μ§μ νμ¬ λ°°μ΄ μ΄κΈ°ν
int[] score1 = {100, 90, 80};
// .length λ©μλ : λ°°μ΄μ κΈΈμ΄ λ¦¬ν΄
// .length λ©μλλ μ½κΈ°μ μ© λ©μλλΌμ κ°μ λ³κ²½μ΄ λΆκ°λ₯
// score1.length = 10; μ΄λ° λ°©μμ κ° λμ
λΆκ°λ₯
for(int i = 0; i < score1.length; i++) {
//λ°°μ΄μ μμ νλνλμ μ κ·Όνλ λ°©μ, λ°°μ΄ μ΄λ¦ [μμμ μΈλ±μ€]
System.out.println(score1[i]);
}
System.out.println("---------------------");
// 2. new ν€μλλ‘ μ΄κΈ°ν
int score2[] = new int[] {75, 85, 100};
for(int j = 0; j < score2.length; j++) {
System.out.println(score2[j]);
}
System.out.println("---------------------");
// 3. new ν€μλλ‘ κΈΈμ΄λ§ μ§μ νκ³ μ΄κΈ°ν
// λ³μ νμ
μ λ°λΌ μ΄κΈ°ν κ°μ΄ λ€λ¦
int[] score3 = new int[3];
String[] str = new String[3];
// μ΄κΈ°νλ λ°°μ΄ λ³μμ κ° λ³κ²½
// λ°°μ΄μ μμμ μ§μ κ° μ
λ ₯
score3[1] = 100;
str[2] = "hello";
for(int k = 0; k < score3.length; k++) {
System.out.println(score3[k]);
}
for(int h = 0; h < str.length; h++) {
System.out.println(str[h]);
}
System.out.println("---------------------");
}
}
λ°°μ΄μ 볡μ¬
- forλ¬Έμ μ΄μ©
int[] score1 = {100, 90, 80};
int[] score2 = new int[5];
for(int i = 0; i < score1.length; i++) {
score2[i] = score1[i]
}
- System.arraycopy( ) λ©μλ μ΄μ©
System.arraycopy(μλ³Έ λ°°μ΄μ μ΄λ¦, 볡μ¬ν μΈλ±μ€, 볡μ¬λ λμμ λ°°μ΄μ μ΄λ¦, μ λ°°μ΄μ 볡μ¬κ° μμλλ μΈλ±μ€, 볡μ¬λ κ°μ)
String[] str1 = {"java", "hello"};
String[] str2 = new String[3];
System.arraycopy(str1, 0, str2, 0, str1.length);
// μ½λ© μ€μ΅
package Day02;
public class _02_copyOfarray {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1. forλ¬Έ μ΄μ©ν λ°°μ΄ λ³΅μ¬
// λ°°μ΄μ μμκ°μ μ§μ μ
λ ₯νμ¬ λ³΅μ¬
int[] score1 = {100, 90, 80};
int[] score2 = new int[5];
for(int i = 0; i < score1.length; i++) {
score2[i] = score1[i];
System.out.println(score2[i]);
}
System.out.println("----------------------");
// 2. arraycopy() λ©μλ μ΄μ©ν λ°°μ΄ λ³΅μ¬
String[] str1 = {"hello", "java"};
String[] str2 = new String[3];
System.arraycopy(str1, 0, str2, 0, str1.length);
for(int j = 0; j < str2.length; j++) {
System.out.println(str2[j]);
}
System.out.println("----------------------");
}
}
ν₯μλ forλ¬Έ
λ°°μ΄μ μ΄μ©νμ¬ μ΄κΈ°νλ λ³μ μμ΄ forλ¬Έ μ¬μ© κ°λ₯
public class _03_advancedFor {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] scores = {100, 90, 80};
int sum = 0;
//scores λ°°μ΄μ μμ κ°μλ§νΌ λ°λ³΅
//scores λ°°μ΄μ μμ νλμ© κΊΌλ΄μ score λ³μμ λ΄μμ μ¬μ©
for(int score : scores) {
sum += score;
}
System.out.println("μ μμ μ΄ν©μ : " + sum );
}
}
LIST
'#1 Language π > 1-4 JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JAVA] κ°μ²΄μ ν΄λμ€ (0) | 2022.11.06 |
---|---|
[JAVA] μ΄κ±° νμ (Enum) (0) | 2022.11.06 |
[JAVA] λ°λ³΅λ¬Έ (0) | 2022.07.21 |
[JAVA] 쑰건문 (0) | 2022.07.21 |
[JAVA] μ°μ°μ (Operator) (0) | 2022.07.20 |