JAVA์˜ ์ •์„ (ch5)

Du-Hyeon, Kimยท2023๋…„ 7์›” 14์ผ
0

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
5/11
post-thumbnail

๐Ÿ’ป 5. Array

๐Ÿฌ array ๊ธฐ๋ณธ

  • array ๋ณ€์ˆ˜์˜ ์„ ์–ธ
    int[] Arr;
    int Arr[];๋„ ๊ฐ€๋Šฅ
int[] Arr;
//๋ฐฐ์—ด ์ฐธ์กฐ๋ณ€์ˆ˜ ์ƒ์„ฑ(๋ฐฐ์—ด ์‹œ์ž‘ ํฌ์ธํ„ฐ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ๋ณด๋ฉด ๋จ)
Arr = new int[5];
//์ง์ ‘์ ์œผ๋กœ ์‹œ์ž‘์ฃผ์†Œ๋ถ€ํ„ฐ data type์— ๋งž๊ฒŒ ๊ณต๊ฐ„ ํ™•๋ณด
int[] Arr = new int[10];
//๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ํ•œ ์ค„๋กœ ๋ฐ”๋กœ ๊ณต๊ฐ„๊นŒ์ง€ ์„ ์–ธํ•จ

๐Ÿฌ๋ฐฐ์—ด ๊ธธ์ด ๋งค์„œ๋“œ

Array.length

๐Ÿฌ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”

๋ฐฐ์—ด์˜ ์ดˆ๊ธฐํ™”

int[] iArr3 = new int[]{100, 95, 80, 70, 60};
int[] iArr3 = {100, 95, 80, 70, 60};
//๋™์ผํ•จ, new int[]์€ ์ƒ๋žต๊ฐ€๋Šฅ

But!

int[] score;
score = {50, 70, 100, 95}
//ERROR!

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

  • for๋ฌธ์œผ๋กœ ๋Œ€๊ฒŒ ๋ฐฐ์—ด ์š”์†Œ๋“ค์„ ์ถœ๋ ฅํ•จ
  • ๋ฐฐ์—ด์˜ ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ printํ•˜๋ฉด
int[] iArr1 = new int[10];
System.out.println(iArr1);
/**
์ถœ๋ ฅ๊ฒฐ๊ณผ : [I@1ee0005
'ํƒ€์ž„@์ฃผ์†Œ'
**/
String[] strArr = {"abc", "akjsbhg"};
System.out.println(strArr);
//์ถœ๋ ฅ๊ฒฐ๊ณผ : [Ljava.lang.String;@6504e3b2

**์˜ˆ์™ธ์‚ฌํ•ญโ—**
char[] chArr = {'a', 'b', 'c', 'd'};
์˜ ๊ฒฝ์šฐ, ์ฆ‰
charํ˜• ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ๋Š” println์—์„œ abcd๊ฐ€ ์ถœ๋ ฅ๋จ!!

๐Ÿฌ๋ฐฐ์—ด ์ถœ๋ ฅ ๋ฐฉ์‹์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ์ฐจ์ด

  • for๋ฌธ ์ถœ๋ ฅ
for(int i=0; i < iArr1.length;i++) {
	System.out.print(iArr1[i]);
	if(i==(iArr1.length-1)) {
		continue;
	}
	System.out.print(",");
}

์ถœ๋ ฅ๊ฒฐ๊ณผ: 1,2,3,4,5,6,7,8,9,10

  • toString ๋งค์„œ๋“œ
System.out.println(Arrays.toString(iArr2));

์ถœ๋ ฅ๊ฒฐ๊ณผ: [6, 6, 10, 7, 2, 7, 7, 7, 2, 1]

์†Œ์†Œํ•œ tips

ํ‰๊ท  ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ TIPS

average = sum / (float)score.length;

์ด๋–„, (float)์œผ๋กœ intํ˜• data type์„ ๋ณ€ํ™˜ํ•จ์œผ๋กœ์„œ ํ‰๊ท ์„ float์œผ๋กœ ํ˜•๋ณ€ํ™˜

๐Ÿฌ๋ฐฐ์—ด shuffle ์˜ˆ์‹œ

import java.util.Arrays;
//Array.toString() ๋งค์„œ๋“œ ์‚ฌ์šฉ์„ ์œ„ํ•ด import

class Ex5_4 {
	public static void main(String[] args) {
		int[] numArr = {0,1,2,3,4,4,5,6,7,8,9};
		System.out.println(Arrays.toString(numArr));

		for (int i=0; i < 100; i++ ) {
			int n = (int)(Math.random() * 10);
			int tmp = numArr[0];
			numArr[0] = numArr[n];
			numArr[n] = tmp;
			//๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์™€ 0~9๊นŒ์ง€์˜ ๋‚œ์ˆ˜ n๋ฒˆ์งธ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ tmp๋ฅผ ์ด์šฉํ•ด์„œ swap
			System.out.println(Arrays.toString(numArr));
		}
		System.out.println(Arrays.toString(numArr));
	} // main
}

์ถœ๋ ฅ๊ฒฐ๊ณผ ์ผ๋ถ€:
[0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9],
[2, 1, 0, 3, 4, 4, 5, 6, 7, 8, 9],
[3, 1, 0, 2, 4, 4, 5, 6, 7, 8, 9],
[1, 3, 0, 2, 4, 4, 5, 6, 7, 8, 9],
[6, 3, 0, 2, 4, 4, 5, 1, 7, 8, 9],
[4, 3, 0, 2, 6, 4, 5, 1, 7, 8, 9]

๐Ÿฌarray data type์— ๋”ฐ๋ฅธ default ์ดˆ๊ธฐํ™” value

  • ๋ฐฐ์—ด์˜ data type์— ๋Œ€ํ•˜์—ฌ new Array[5]์— ๋”ฐ๋ฅธ ์ดˆ๊ธฐํ™” ๊ฐ’์ด ๋‹ค๋ฅด๋‹ค.
typevalue
booleanfalse
char'\u0000'
byte, short, int0
long0L
float0.0f
double0.0d or 0.0
์ฐธ์กฐํ˜•null

๐Ÿฌ String class arry declaration

  • String์€ ํƒ€์ž…์ด ํด๋ž˜์Šค์ด๋ฏ€๋กœ,
String[] names = {"Kim", "Park", "Yi"};
String[] names;
names[0] = new String("Kim");
์ด ๊ทผ๋ณธ
names[0] = "Kim";
์ด๊ฒƒ๋„ ํ—ˆ์šฉ๋œ ํ‘œ๊ธฐ

์‚ฌ์‹ค์ƒ String์€ char๋ฐฐ์—ด์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด๋„๋จ
์ด๋–„, char๋ฐฐ์—ด์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ธฐ๋Šฅ(๋งค์„œ๋“œ)์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํ™•์žฅํ•œ ํด๋ž˜์Šค์ž„

๐ŸฌString class ์ฃผ์š” methods

๋ฐ˜ํ™˜ ํƒ€์ž…๋งค์„œ๋“œ ์ด๋ฆ„์„ค๋ช…
charcharAt(int index)ํ•ด๋‹น index char ๋ฐ˜ํ™˜
intlength()array length int ๋ฐ˜ํ™˜
Stringsubstring(int from, int to)from ~ to(์ œ์™ธ) string ๋ฐ˜ํ™˜
booleanequals(Object obj)๋ฌธ์ž์—ด ๋™์ผ ์—ฌ๋ถ€ ํ™•์ธ
char[]toCharArray()๋ฌธ์ž์—ด์„ char๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ char[]๋กœ ๋ฐ˜ํ™˜

๐ŸฌCommand line args delivery

  • ์šฐ๋ฆฌ๊ฐ€ java์ฝ”๋“œ์— ์‚ฌ์šฉํ–ˆ๋˜ public static void main(String[] agrs)๋Š”
    ์ปค๋งจ๋“œ ๋ผ์ธ์„ ํ†ตํ•ด์„œ args ์ฐธ์กฐ์ž์˜ string ๋ฐฐ์—ด์˜ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•œ๋‹ค.
class Ex5_7 {
	public static void main(String[] args) {
		System.out.println("length of args:"+args.length);
		for(int i=0;i< args.length;i++) {
			System.out.println("args[" + i + "] = \""+ args[i] + "\"");
		}
	}
}
command line: 
c:\jdk1.8\work\ch5>java MainTest abc 123 "Hello world"(์ฑ…)

๋‚ด pc ์‹คํ–‰๊ฒฐ๊ณผ(cygwin)

๐Ÿฌ2์ฐจ์› ๋ฐฐ์—ด ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”

int[][] Arr2;
int [][]Arr2 = new int[4][3]
4x3 ๋ฐฐ์—ด (ํ–‰x์—ด)
 int[][] score = {
	  { 100, 100, 100}
	, { 20, 20, 20}
	, { 30, 30, 30}
	, { 40, 40, 40}
	, { 50, 50, 50}
};
์ด๊ฑฐ ๋ณด์ด๋Š” ๋Œ€๋กœ ํ–‰์—ด์ธ ์„ ์–ธ์ž„(ํŽธํ•จ)

๐Ÿฌ๋” ๋‹ค์–‘ํ•œ Arrays methods

  • 2์ฐจ์› ๋ฐฐ์—ด ๋น„๊ต ๋งค์„œ๋“œ(๋ชจ๋“  ์‹œ์ž‘์€ Arrays.๋งค์„œ๋“œ)
Arrays.equlas(arr, arr1)๋Š” ์ผ์ฐจ์› ๋ฐฐ์—ด์—์„œ๋งŒ ๊ฐ€๋Šฅ
Arrays.deepEquals(strr, strr2D)๋Š” ๋‹ค์ฐจ์›์—์„œ๋„ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด ๋ณต์‚ฌ ๋งค์„œ๋“œ
int[] arr={0, 1, 2, 3, 4};
int[] arr2 = Arrays.copyOf(arr, arr.length);
//์ „์ฒด ๋ณต์‚ฌ
int[] arr3 = Arrays.copyOf(arr, 3);
//์•ž์— 3๊ฐœ๋งŒ ๋ณต์‚ฌ
int[] arr2 = Arrays.copyOfRange(arr, 2, 4);
//index 2๋ถ€ํ„ฐ 3๊นŒ์ง€ ๋ณต์‚ฌ
//range๊ฐ€ ๋ฐฐ์—ด ์š”์†Œ ์ด์ƒ์ด๋ฉด 0(int)์œผ๋กœ ์ดˆ๊ธฐํ™”
  • ๋ฐฐ์—ด์˜ ์ •๋ ฌ
Arrays.sort(arr);

arr๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋จ

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