- 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์ผ๋ก ํ๋ณํ
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]
- ๋ฐฐ์ด์ data type์ ๋ํ์ฌ new Array[5]์ ๋ฐ๋ฅธ ์ด๊ธฐํ ๊ฐ์ด ๋ค๋ฅด๋ค.
type | value |
---|---|
boolean | false |
char | '\u0000' |
byte, short, int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d or 0.0 |
์ฐธ์กฐํ | null |
- String์ ํ์ ์ด ํด๋์ค์ด๋ฏ๋ก,
String[] names = {"Kim", "Park", "Yi"}; String[] names; names[0] = new String("Kim"); ์ด ๊ทผ๋ณธ names[0] = "Kim"; ์ด๊ฒ๋ ํ์ฉ๋ ํ๊ธฐ
์ฌ์ค์ String์ char๋ฐฐ์ด์ด๋ผ๊ณ ์๊ฐํด๋๋จ
์ด๋, char๋ฐฐ์ด์ ์ฌ๋ฌ๊ฐ์ง ๊ธฐ๋ฅ(๋งค์๋)์ ์ถ๊ฐํ์ฌ ํ์ฅํ ํด๋์ค์
๋ฐํ ํ์ | ๋งค์๋ ์ด๋ฆ | ์ค๋ช |
---|---|---|
char | charAt(int index) | ํด๋น index char ๋ฐํ |
int | length() | array length int ๋ฐํ |
String | substring(int from, int to) | from ~ to(์ ์ธ) string ๋ฐํ |
boolean | equals(Object obj) | ๋ฌธ์์ด ๋์ผ ์ฌ๋ถ ํ์ธ |
char[] | toCharArray() | ๋ฌธ์์ด์ char๋ฐฐ์ด๋ก ๋ณํํด์ char[]๋ก ๋ฐํ |
- ์ฐ๋ฆฌ๊ฐ 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)
์ด๊ฑฐ ๋ณด์ด๋ ๋๋ก ํ์ด์ธ ์ ์ธ์(ํธํจ)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} };
- 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๊ฐ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋จ