단일배열
생성 : 자료형[] 변수명 = new 자료형[갯수];
호출 : 변수명[인덱스]
인덱스 : 0부터 시작, 연속된다. ==갯수-1
점프를 안한곳(0)부터 몇번 점프할 것이냐
package com.bit.day06;
public class Ex01 {
public static void main(String[] args) {
/* su[0]=1;
su[1]=3;
su[2]=5;
su[3]=7;
su[4]=9;
*/
// int su[] = new int[]{1,3,5,7,9};
int[] su={1,3,5,7,9}; //경우에 따라 허용하는 문법
for(int i=0; i<5; i++){
System.out.println(su[i]);
}
}
}
su = 포인터의 역할을 하는 것이다. 즉 참조변수이다.(주소를 참조하는 것)
자바는 포인터의 개념이 없기 때문에 배열을 객체로 찍어낸다.(힙영역 사용) = 배열객체
그렇기때문에 value값 비교가아니라 reference 비교이다. --> 경우에 따라서 원하는 결과가 안나올 수도 있다.
package com.bit.day06;
public class Ex02 {
public static void main(String[] args) {
int[] arr1=new int[]{1,3,5,7}; //배열 객체 생성
int[] arr2=new int[]{1,3,5,7}; //다른 배열 객체 생성
int[] arr3={1,3,5,7}; //또 하나의 배열 객체 생성
int[] arr4=arr1; //배열의 얕은 복사
arr1[0]=2;
System.out.println(arr1==arr2); //참조변수형이기 때문에 false
System.out.println(arr1==arr3); //똑같이 false
System.out.println(arr1==arr4); //true 출력
for(int i=0; i<arr4.length; i++){
System.out.println(arr4[i]); //동일 객체이기 때문에 2,3,5,7 출력
}
}
}
package com.bit.day06;
public class Ex03 {
public static void func(int[] arr2){
for(int i=0; i<arr2.length; i++){
arr2[i]+=1;
}
}
public static void main(String[] args) {
int[] arr1={2,4,6,8};
System.out.println("before...");
for(int i=0; i<arr1.length; i++){
System.out.print(arr1[i]+" "); //2 4 6 8 출력
}
System.out.println();
func(arr1); //스택에 메인 위에 func 메소드 쌓임, 배열을 전달 = 복사. 메인에서 선언한 arr1은 지역변수.
// 여기서 얕은 복사가 일어남
System.out.println("after...");
for(int i=0; i<arr1.length; i++){
System.out.print(arr1[i]+" ");
}
System.out.println(); //3 5 7 9 출력
}
}
package com.bit.day06;
public class Ex04 { //깊은 복사
public static void main(String[] args) {
int[] arr1={1,3,5,7};
int[] arr2;
arr2=new int[4];
for(int i=0; i<arr1.length; i++){
arr2[i]=arr1[i];
}
arr1[0]=1234; //arr2의 원소는 바뀌지 않는다.
for(int i=0; i<arr2.length; i++){
System.out.print(arr2[i]+" ");
}
}
}
package com.bit.day06;
public class Ex05 {
public static void main(String[] args){
int[] arr1={2,4,6,8,10};
int[] arr2=new int[5];
// 깊은복사기능을 제공 arraycopy메소드
// arraycopy(원본,원본시작 index, 복사될 결과물, 결과물 시작 index, 시작시점으로부터 몇개)
System.arraycopy(arr1, 0, arr2, 0, 5); //인자를 전달하는 것은 얕은복사.
for(int i=0; i<arr2.length; i++){
System.out.println(arr2[i]+" ");
}
}
}
package com.bit.day06;
public class Ex05 {
public static void main(String[] args){
int[] arr1={11,22,33,44};
int[] arr2={1,2,3,4,5,6,7,8,9};
System.arraycopy(arr1, 0, arr2, 2, 4); //기능 : 깊은 복사를 수행한다.
for(int i=0; i<arr2.length; i++){
System.out.print(arr2[i]+" ");
}
}
}
package com.bit.day06;
public class Ex06 {
public static void main(String[] args){
int[] arr1={1,3,5,7,9};
int[] arr2;
arr2=java.util.Arrays.copyOf(arr1, 5); //Arrays는 클래스. java.util이라는 패키지 안에 있는 것이다.
for(int i=0; i<arr2.length; i++){
System.out.println(arr2[i]+" ");
}
}
}
package com.bit.day06;
import javax.tools.JavaCompiler;
public class Ex06 {
public static void main(String[] args){
int[] arr1={1,3,5,7,9};
int[] arr2;
arr2=java.util.Arrays.copyOf(arr1, 5);
/* for(int i=0; i<arr2.length; i++){
System.out.println(arr2[i]+" ");
}
*/
//반복문을 돌리지 않아도 볼 수 있음.
System.out.println(java.util.Arrays.toString(arr2)); //[1,3,4,7,9] 출력
}
}
package com.bit.day06;
import javax.tools.JavaCompiler;
public class Ex06 {
public static void main(String[] args){
int[] arr1={1,3,5,7,9};
int[] arr2;
int[] arr3={11,22,33,44,55,66,77,88,99};
arr2=java.util.Arrays.copyOf(arr1, 5);
arr3=java.util.Arrays.copyOfRange(arr1, 1, 4); // 3,5,7 출력
System.out.println(java.util.Arrays.toString(arr3));
}
}
package com.bit.day06;
public class Ex07 {
public static void func(int[] arr1){
System.out.println("call Ex07 func()...");
}
public static void main(String[] args) {
int[] arr={};
Ex07.func(arr); //숨겨져있는 클래스명
}
}
다른 패키지의 클래스 함수 이용시
package com.bit.day06.pm;
public class Ex01 {
public static void func(int[] arr){
System.out.println("call Ex01 func()...");
}
public static void main(String[] args) {
int[] arr={};
com.bit.day06.Ex07.func(arr); //다른 패키지의 클래스
}
}
String, System과 같은 클래스 또한 패키지가 존재한다.
java.lang 패키지 - 패키지명을 명세하지 않고 사용(생략 가능)
다중배열
: 배열을 담는 배열
package com.bit.day07;
public class Ex01 {
public static void main(String[] args) {
int[] su1=new int[]{1,2,3};
int[] su2=new int[]{4,5,6};
int[] su3=new int[]{7,8,9};
//다중배열
//자료형[][] 변수명 = new 자료형[size][value의 size];
int[][] su4=new int[4][3];
su4[0]=su1;
su4[1]=su2;
su4[2]=su3;
su4[3]=new int[]{10,11,12};
for(int i=0; i<su4.length; i++){ //su4는 4개의 배열을 담고있다. => su4.length=4
int[] temp=su4[i];
for(int j=0; j<temp.length; j++){ //temp.length = value의 size
System.out.print(temp[j]+"\t");
}
System.out.println();
}
}
}
for(int i=0; i<su4.length; i++){
for(int j=0; j<su4[i].length; j++){
System.out.print(su4[i][j]+"\t");
}
System.out.println();
}
public class Ex02 {
public static void main(String[] args) {
int[][] arr1=new int[][]{
new int[]{1,2,3},
new int[]{4,5,6},
new int[]{7,8,9}
};
int[][] arr2={{1,2,3},{4,5,6},{7,8,9}};
//이상하게 출력됨. 제일 바깥쪽만 풀어서 보여준 것이다. for문이라면 바깥쪽 for문만 출력해준 것과 같다.
System.out.println(java.util.Arrays.toString(arr1));
}
}
int[][] arr2={{1,2,3},{4,5,6},{7,8,9}};
for(int i=0; i<arr2.length; i++){
System.out.println(java.util.Arrays.toString(arr2[i]));
}
}
배열 = 참조변수 = 객체를 찍는 주소값 => 초기화 => 값을 안주면 default값 null로 초기화
다중배열 - 배열의 주소를 갖고있다. 아직 배열을 생성하지 않았다면 객체가 없는 것이기 때문에 주소값이 null이 나오는 것이다. 그곳에 어떤 숫자라도 쓰는 순간 그 주소를 가리키기 때문이다.(0일지라도)
element 사이즈가 없다 = 객체가 아직 안만들어졌다 --> 가리킬 주소가 없다 = null
public class Ex04 {
public static void main(String[] args) {
int[][] arr1=new int[3][2]; //만들어놓고 밑에서 치환해버리니까 필요없는 객체가 생긴다--> [3][] 로 바꿀 수 있다.--> null값으로 바뀌는 것
//그러나 arr1[1]을 없애고 싶을 경우,[3][]이면 null오류가 뜨기 때문에 일단 default값 0을 주는 것이 좋다 -->[3][2]형태.
//즉, 밑에서 값을 줘버릴 경우엔 없는 것이 효율적. 선언을 해놓고 특정 부분만 바꿔서 출력하고 싶다면 있는 것이 문제 없다.
arr1[0]=new int[]{1,2};
arr1[1]=new int[]{6,7,8,9}; // int 배열만 들어가면 되니까 배열의 사이즈가 2라는 보장이 없다.
arr1[2]=new int[]{1,2,3};
for(int i=0; i<arr1.length; i++){
int[] temp=arr1[i];
for(int j=0; j<temp.length; j++){
System.out.print(temp[j]+"\t");
}
System.out.println();
}
}
}