Java์ ๋ฐฐ์ด(Arrays)์ ์ฌ๋ฌ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ ๊ธฐ๋ณธ์ ์ธ ์๋ฃ ๊ตฌ์กฐ๋ก, ์ค์ํ์ ๋ค์ํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์ด๋ฒ ๊ธ์์๋ ๋ฐฐ์ด์ด ์ด๋ป๊ฒ ์ค์ํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ํ์ฉ๋ ์ ์๋์ง, ๋ค์ํ ์ค์ ์ฌ๋ก๋ฅผ ํตํด ์์๋ณด๊ฒ ์ต๋๋ค.
1. ํ์ ์ฑ์ ๊ด๋ฆฌ
ํ๊ต์์ ํ์๋ค์ ์ฑ์ ์ ๊ด๋ฆฌํ ๋, ๋ฐฐ์ด์ ์ฌ์ฉํ๋ฉด ํจ์จ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
์์ : ํ์ ์ฑ์ ํ๊ท ๊ณ์ฐ
public class StudentScores {
public static void main(String[] args) {
int[] scores = {85, 90, 78, 92, 88};
int sum = 0;
for (int score : scores) {
sum += score;
}
double average = (double) sum / scores.length;
System.out.println("ํ๊ท ์ฑ์ : " + average);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
ํ๊ท ์ฑ์ : 86.6
์ด ์์ ๋ ํ์๋ค์ ์ฑ์ ์ ๋ฐฐ์ด์ ์ ์ฅํ๊ณ , ํ๊ท ์ ๊ณ์ฐํ์ฌ ์ถ๋ ฅํฉ๋๋ค.
2. ์ผํ๋ชฐ ์ฅ๋ฐ๊ตฌ๋
์จ๋ผ์ธ ์ผํ๋ชฐ์์ ๊ณ ๊ฐ์ ์ฅ๋ฐ๊ตฌ๋์ ๋ด๊ธด ์ํ ๋ชฉ๋ก์ ๊ด๋ฆฌํ๋ ๋ฐ ๋ฐฐ์ด์ด ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
์์ : ์ฅ๋ฐ๊ตฌ๋ ์ด์ก ๊ณ์ฐ
public class ShoppingCart {
public static void main(String[] args) {
double[] prices = {19.99, 45.50, 12.75, 7.99};
double total = 0;
for (double price : prices) {
total += price;
}
System.out.println("์ฅ๋ฐ๊ตฌ๋ ์ด์ก: $" + total);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์ฅ๋ฐ๊ตฌ๋ ์ด์ก: $86.23
์ด ์ฝ๋๋ ์ํ ๊ฐ๊ฒฉ์ ๋ฐฐ์ด๋ก ์ ์ฅํ๊ณ , ์ด๋ฅผ ์ํํ๋ฉฐ ์ด์ก์ ๊ณ์ฐํฉ๋๋ค.
3. ์ํ ๊ณ์ข ์์ก ๊ด๋ฆฌ
์ํ์์ ๊ณ ๊ฐ ๊ณ์ข์ ์์ก์ ๊ด๋ฆฌํ๋ ๋ฐ ๋ฐฐ์ด์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์์ : ๋ชจ๋ ๊ณ์ข์ ์ด ์์ก ๊ณ์ฐ
public class BankAccounts {
public static void main(String[] args) {
double[] balances = {1200.50, 2500.75, 320.00, 5000.00, 750.25};
double totalBalance = 0;
for (double balance : balances) {
totalBalance += balance;
}
System.out.println("์ด ์์ก: $" + totalBalance);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์ด ์์ก: $9771.5
์ด ํ๋ก๊ทธ๋จ์ ๋ชจ๋ ๊ณ์ข์ ์์ก์ ๋ฐฐ์ด๋ก ์ ์ฅํ๊ณ , ์ด๋ฅผ ํฉ์ฐํ์ฌ ์ด ์์ก์ ๊ณ์ฐํฉ๋๋ค.
4. ์ํ ํ์ ๋ถ์
์ํ ๋ฆฌ๋ทฐ ์ฌ์ดํธ์์ ์ฌ์ฉ์ ํ์ ์ ๋ฐฐ์ด๋ก ์ ์ฅํ๊ณ , ์ต๊ณ ํ์ ์ ๊ณ์ฐํ๋ ๋ฐ ๋ฐฐ์ด์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์์ : ์ต๊ณ ํ์ ์ฐพ๊ธฐ
public class MovieRatings {
public static void main(String[] args) {
double[] ratings = {4.5, 3.8, 4.9, 2.7, 4.6};
double maxRating = ratings[0];
for (double rating : ratings) {
if (rating > maxRating) {
maxRating = rating;
}
}
System.out.println("์ต๊ณ ํ์ : " + maxRating);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์ต๊ณ ํ์ : 4.9
์ด ํ๋ก๊ทธ๋จ์ ๋ฐฐ์ด์ ์ ์ฅ๋ ํ์ ์ ์ํํ๋ฉฐ ๊ฐ์ฅ ๋์ ํ์ ์ ๊ณ์ฐํฉ๋๋ค.
5. IoT ์ผ์ ๋ฐ์ดํฐ ๋ถ์
์ค๋งํธ ํ์ ์จ๋ ์ผ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐฐ์ด์ ์ ์ฅํ๊ณ , ํน์ ๊ธฐ๊ฐ์ ํ๊ท ์จ๋๋ฅผ ๊ณ์ฐํ ์ ์์ต๋๋ค.
์์ : ์ผ์ฃผ์ผ๊ฐ ํ๊ท ์จ๋ ๊ณ์ฐ
public class TemperatureAnalysis {
public static void main(String[] args) {
double[] temperatures = {22.5, 23.0, 21.8, 22.1, 22.9, 23.3, 21.7};
double sum = 0;
for (double temp : temperatures) {
sum += temp;
}
double average = sum / temperatures.length;
System.out.println("์ผ์ฃผ์ผ๊ฐ ํ๊ท ์จ๋: " + average + "°C");
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์ผ์ฃผ์ผ๊ฐ ํ๊ท ์จ๋: 22.47142857142857°C
์ด ์ฝ๋๋ ๋ฐฐ์ด์ ์ ์ฅ๋ ์จ๋ ๋ฐ์ดํฐ๋ฅผ ์ํํ๋ฉฐ ํ๊ท ์จ๋๋ฅผ ๊ณ์ฐํฉ๋๋ค.
6. ์ฌํ ์ผ์ ๊ด๋ฆฌ
์ฌํ ์ผ์ ์ค ๋ฐฉ๋ฌธํ ๋์ ๋ชฉ๋ก์ ๋ฐฐ์ด๋ก ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
์์ : ๋์ ์์๋๋ก ์ถ๋ ฅ
public class TravelItinerary {
public static void main(String[] args) {
String[] cities = {"Seoul", "Tokyo", "Paris", "New York", "London"};
for (String city : cities) {
System.out.println("๋ฐฉ๋ฌธ ์์ ๋์: " + city);
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
๋ฐฉ๋ฌธ ์์ ๋์: Seoul
๋ฐฉ๋ฌธ ์์ ๋์: Tokyo
๋ฐฉ๋ฌธ ์์ ๋์: Paris
๋ฐฉ๋ฌธ ์์ ๋์: New York
๋ฐฉ๋ฌธ ์์ ๋์: London
์ด ํ๋ก๊ทธ๋จ์ ๋ฐฐ์ด์ ์ ์ฅ๋ ๋์ ์ด๋ฆ์ ์์๋๋ก ์ถ๋ ฅํฉ๋๋ค.
๊ฒฐ๋ก
Java ๋ฐฐ์ด์ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ณ ๋ค์ํ ์ค์ํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ํ์ ์ฑ์ ๊ด๋ฆฌ, ์ผํ๋ชฐ ์ฅ๋ฐ๊ตฌ๋, ์ํ ๊ณ์ข ๊ด๋ฆฌ, ์ํ ํ์ ๋ถ์, IoT ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ฑ ์ฌ๋ฌ ๋ถ์ผ์์ ๋ฐฐ์ด์ ํ์ฉํ ์ ์์ต๋๋ค. ์ด๋ฒ ๊ธ์์ ์๊ฐํ ์์ ๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ฐฐ์ด์ ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ตํ๋ณด์ธ์!
๋๊ธ ์ฐ๊ธฐ