๐Ÿ“˜ [ JAVA ๋ฐฐ์—ด ]

rossiebiยท2022๋…„ 12์›” 6์ผ
0

๐Ÿ’ป JAVA ๊ฐœ๋ฐœ์ผ์ง€ย 

๋ชฉ๋ก ๋ณด๊ธฐ
29/38
post-thumbnail

โญ ๋ฐฐ์—ด

  • ๊ฐ™์€ ํƒ€์ž…์˜ ๋ณ€์ˆ˜๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์œ ํ•œ ์ง‘ํ•ฉ
  • ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ๊ธฐ
  • ๋ฐฐ์—ด์€ ๊ฐ™์€ ํƒ€์ž…์˜ ๊ฐ’๋งŒ ๊ด€๋ฆฌ

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);
		
		}
	}

๐Ÿ’ฅ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค

  • ๊ฐ ์š”์†Œ์— ์ž๋™์œผ๋กœ ๋ถ™๋Š” ์ผ๋ จ๋ฒˆํ˜ธ
  • ์ธ๋ฑ์Šค์˜ ๋ฒ”์œ„๋Š” 0๋ถ€ํ„ฐ '๋ฐฐ์—ด๊ธธ์ด -1๊นŒ์ง€'

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]);
		}
	}
											
}

๐Ÿ’ฅ ๋ฐฐ์—ด์˜ ์ดˆ๊ธฐํ™”

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ์ฒ˜์Œ์œผ๋กœ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ
  • ๋ฐฐ์—ด์€ ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ์ž๋™์ ์œผ๋กœ ๊ธฐ๋ณธ๊ฐ’์ธ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋จ
  • ์›ํ•˜๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๋ ค๋ฉด ๊ฐ’์„ ์ง€์ •ํ•ด ์ดˆ๊ธฐํ™” ํ•ด์•ผํ•จ!

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[] ์ƒ๋žต ๊ฐ€๋Šฅ

๐Ÿ’ฅ ๋ฐฐ์—ด์˜ ์ถœ๋ ฅ

  • ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•˜๋ ค๋ฉด for๋ฌธ ์‚ฌ์šฉ!
  • sysout์œผ๋กœ ๋ฐ”๋กœ ์ถœ๋ ฅ ๋ถˆ๊ฐ€

๐ŸŒฑ ์˜ˆ์ œ

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);

	}

}
profile
๋น„์ „๊ณต์ž ๊ฐœ๋ฐœ์ž๋ฅผ ๊ฟˆ๊พธ๋Š” ๋กœ์ง€

0๊ฐœ์˜ ๋Œ“๊ธ€