ex) ํ์ ์ฑ์ ์ int ํ์ ๋ฐฐ์ด์ ๋ด๊ธฐ
int[] scores = { 65, 74, 23, 75, 68, 96, 88, 98, 54 };
// 1) ์์ฑ๊ณผ ๋์์ ์ด๊ธฐํ
double[] arr = { 1.5, 3.2, 4.3, 0.9 }; // double ํ ๋ฐฐ์ด ์์ฑ
String[] names = { "Tom", "Kate", "Paul", "James" }; // String ๋ฐฐ์ด ์์ฑ
// 2) ๊ณต๊ฐ ํ ๋น ํ ๊ฐ ๋์
int[] ids = new int[3]; // intํ ๋ฐฐ์ด ids ๊ณต๊ฐ ํ ๋น
ids[0] = 100; // ๊ฐ ๋์
ids[1] = 200; // ๊ฐ ๋์
ids[2] = 300; // ๊ฐ ๋์
1) ๋ฐฐ์ด ๊ฐ ์ฝ๊ธฐ(read)
int[] scores = {99, 88, 77};
System.out.println(scores[0]); // 99
System.out.println(scores[1]); // 88
System.out.println(scores[2]); // 77
2) ๋ฐฐ์ด ๊ฐ ๋ณ๊ฒฝ(write)
System.out.println(scores[0]); // 99
scores[0] = 0; // 0๋ฒ ์ธ๋ฑ์ค ๊ฐ ๋ณ๊ฒฝ
System.out.println(scores[0]); // 0
package practice01;
public class Test4 {
public static void main(String[] args) { //๋ฉ์ธ๋ฉ์๋
// ๋ฐฐ์ด ๋ณ์ ์ ์ธ๊ณผ ๋ฐฐ์ด ์์ฑ
String [] season = {"๋ด", "์ฌ๋ฆ", "๊ฐ์", "๊ฒจ์ธ"};
//๋ฐฐ์ด์ ํญ๋ชฉ๊ฐ ์ฝ๊ธฐ
System.out.println("season[0]:" +season[0]);
System.out.println("season[1]:" + season[1]);
System.out.println("season[2]:" + season[2]);
System.out.println("season[3]:" + season[3]);
//์ธ๋ฑ์ค 1๋ฒ์ ํญ๋ชฉ ๊ฐ ๋ณ๊ฒฝ
season[1] = "summer";
System.out.println("season[1]:" + season[1]);
System.out.println();
//๋ฐฐ์ด ๋ณ์ ์ ์ธ๊ณผ ๋ฐฐ์ด ์ ์ธ
int[] score = {80, 97, 54};
// ์ดํฉ๊ณผ ํ๊ท ๊ตฌํ๊ธฐ
int sum = 0;
for(int i=0; i<3; i++) {
sum += score[i];
}
System.out.println("์ดํฉ:" + sum);
double avg = (double)sum/3;
System.out.println("ํ๊ท :" + avg);
}
}
ex) int [] score = new int[5] // ๊ธธ์ด๊ฐ 5์ธ int ๋ฐฐ์ด, score[0] ~ score[4]
public class test1 {
public static void main(String[] args) {
// int [] score; // 1. ๋ฐฐ์ด score ์ ์ธ (์ฐธ์กฐ๋ณ์)
// score = new int[5]; // 2. ๋ฐฐ์ด์ ์์ฑ
int [] score = new int [5]; // ๋ฐฐ์ด์ ์ ์ธ๊ณผ ์์ฑ์ ๋์์
score[3] = 100;
System.out.println("score[3]:" +score[3]);
}
}
๋ฐฐ์ด๋ณ์.length
ex) int [] arr = new int [5]; //๊ธธ์ด 5์ธ int ๋ฐฐ์ด
int tmp = arr.length; // arr.length์ ๊ฐ์ 5์ด๊ณ , tmp์ 5๊ฐ ์ ์ฅ๋จ
public class test1 {
public static void main(String[] args) {
int [] arr = new int[5]; // ๊ธธ์ด๊ฐ 5์ธ int๋ฐฐ์ด arr์ ์์ฑ
System.out.println("arr.length=" + arr.length);
for(int i=0; i<5; i++) {
System.out.println("arr["+i+"]=" + arr[i]);
}
}
}
ex)
int [] score = new int[5];
score[0] = 50;
score[1] = 60;
score[2] = 70;
score[3] = 80;
score[4] = 90;
ex) ๊ฐ๋จํ ํ๋ฉด
int[] score = {50, 60, 70, 80, 90}; //new int[] ์๋ต ๊ฐ๋ฅ
public class test1 {
public static void main(String[] args) {
int [] arr = {100, 90, 80,70}; // ๋ฐฐ์ด ์ด๊ธฐํ
// System.out.println(arr); // ๋ถ๊ฐ
for(int i =0; i<arr.length; i++) {
System.out.println(arr[i]);
}
}
}
import java.util.Arrays;
import java.util.Iterator;
public class test1 {
public static void main(String[] args) {
int[] arr = { 100, 90, 80, 70, 60 }; // ๊ธธ์ด๊ฐ 5์ธ int ๋ฐฐ์ด
System.out.println(Arrays.toString(arr));
}
}
import java.util.Arrays;
import java.util.Iterator;
public class test1 {
public static void main(String[] args) {
// ์ดํฉ๊ณผ ํ๊ท ๊ตฌํ๊ธฐ ์์
int sum = 0; //์ดํฉ์ ์ ์ฅํ๊ธฐ ์ํ ๋ณ์
float avg = 0f; // ํ๊ท ์ ์ ์ฅํ๊ธฐ ์ํ ๋ณ์
int score [] = {100, 88, 100, 100, 90};
for(int i = 0; i<score.length; i++) {
sum += score[i];
}
avg = sum / (float)score.length;
System.out.println("์ดํฉ:" + sum);
System.out.println("ํ๊ท :" + avg);
}
}
public class test1 {
public static void main(String[] args) {
// ์ต๋๊ฐ, ์ต์๊ฐ
int[] score = { 79, 88, 91, 33, 100, 55, 95 };
int max = score[0]; // ๋ฐฐ์ด์ ์ฒซ๋ฒ์งธ ๊ฐ์ผ๋ก ์ต๋๊ฐ์ ์ด๊ธฐํ
int min = score[0]; // ๋ฐฐ์ด์ ์ฒซ๋ฒ์งธ ๊ฐ์ผ๋ก ์ต์๊ฐ์ ์ด๊ธฐํ
for (int i = 0; i < score.length; i++) {
if (score[i] > max) {
max = score[i];
} else if (score[i] < min) {
min = score[i];
}
}
System.out.println("์ต๋๊ฐ:" + max);
System.out.println("์ต์๊ฐ:" + min);
}
}