⚡ 2차 원시배열
package oop; //2차 원시배열 public class array4 { public static void main(String[] args) { String user[][] = { {"홍길동","이순신","유관순"}, //아래로 쭉 데이터 갯수 통일 {"100","88","75"}, {"*","**","***"} }; //System.out.println(user[1][1]); //88출력 //그룹 int gp = user.length; //그룹 갯수 int ea = user[0].length; //해당 그룹의 데이터 갯수 int f,ff; for(f=0; f<ea; f++) { //데이터 갯수만큼 돌림 for(ff=0; ff<gp; ff++) { //그룹 갯수만큼 돌림 System.out.println(user[ff][f]); } } } }
🔽 2차 배열에 데이터삽입int adata[][] = new int[2][2]; //그룹갯수 : 3개 adata[0][0] = 50; adata[0][1] = 100; adata[1][0] = 200; adata[1][1] = 500; //System.out.println(Arrays.toString(adata));//2차배열은 출력x - 특수문자(암호화가 적용) //ddepToString : 2차 배열부터 정적으로 데이터럴 출력시킴 System.out.println(Arrays.deepToString(adata)); //[[50, 100], [200, 500]] 출력
⚡ 2차 배열을 이용한 데이터 응용편
-로그인
package oop; //2차 배열을 이용한 데이터 응용편 public class arrays5 { public static void main(String[] args) { ay5_box ab = new ay5_box(); ab.abc(); } } class ay5_box{ //회원정보 데이터를 배열화 하며, 상수로 적용함(수정 x) final String data[][] = { {"hong","kim","park","jang"}, {"a1234","b1234","c1234","abc1234"}, {"1000","1500","2000","4500"} }; public void abc() { String id = "jang"; String pw = "abc1234"; int count = 0; int gp = this.data.length; //그룹갯수 int ea = this.data[0].length; //첫번째 그룹 데이터 갯수 int f,ff; for(f=0; f<ea; f++) { if(id == this.data[0][f]) { //0 그룹의 데이터(아이디)검토 for(ff=0; ff<gp; ff++) { if(pw == this.data[ff][f]) { //패스워드 검토 System.out.println("로그인 하셨습니다"); System.out.println(this.data[2][f]); count++; break; } } } } if(count == 0) { System.out.println("아이디 패스워드를 확인하세요."); } } }
- 응용package oop; import java.util.Arrays; //사용자 데이터를 이용한 배열값 출력하기 public class array6t { /* {"김민지","서은진","김선숙","명동건","강휘"}, {"A","B","AB","O","B"}, {"SKT","KT","LGT","LGT","KT"} 1. 사용자 중 KT고객명만 출력시키시오. //서은진,강휘 */ public static void main(String[] args) { String info[][] = { {"김민지","서은진","김선숙","명동건","강휘"}, {"A","B","AB","O","B"}, {"SKT","KT","LGT","LGT","KT"} }; ar6t_box ar6 = new ar6t_box(); ar6.abc(info); //abc라는 메소드로 배열 변수를 인자값으로 전달 } } class ar6t_box{ public void abc(String z[][]) { //자료형 배열로 인자값을 받음 int f=0; do { if(z[2][f] == "KT") { System.out.println(z[0][f]); } f++; }while(f<z[0].length); } }
⚡ 1차배열 -> 2차배열로 변환
package oop; import java.util.Arrays; public class array7 { public static void main(String[] args) { String area[] = {"서울","경기도","인천"};//1차배열 String people[] = {"12000","6500","3300"}; //1차배열 //2차배열 생성(그룹 갯수는 고정되어있음) String total[][] = new String[area.length][2]; int f; for(f=0; f<area.length; f++) { //1차배열 데이터 갯수만큼 반봅ㄱ total[f][0] = area[f]; //그룹0 데이터를 이관함 total[f][1] = people[f]; //그룹1 데이터를 이관함 } System.out.println(Arrays.deepToString(total)); //2차배열 생성완료 출력 //[[서울, 12000], [경기도, 6500], [인천, 3300]] 출력 } }
-응용문제(1차배열 -> 2차배열 변환 응용편)/* {90,22,16,13,8,42,49,37,1,9} 해당 1차 배열에 짝수 5개,홀수5개가 있습니다. 2차배열로 각각 짝수그룹,홀수그룹으로 나누어서 출력되도록 코드를 제작합니다. */ int base[] = {90,22,16,13,8,42,49,37,1,9}; int result[][] = new int[2][base.length/2]; //가상번호 int c1=0,c2=0; int s; for(s=0; s<base.length; s++) { if(base[s]%2 == 0) { //짝수 result[0][c1] = base[s]; c1++; }else { //홀수 result[1][c2] = base[s]; c2++; } } System.out.println(Arrays.deepToString(result));
'CLASS > JAVA' 카테고리의 다른 글
#6-1 / 데이터 반환,외부 원시배열 데이터 반환 (0) | 2024.05.08 |
---|---|
#5-3 / 다차원 배열,return 메소드로 배열받기 (0) | 2024.05.07 |
#5-1 / 1차 원시 배열 (0) | 2024.05.07 |
#4-3 / Method (0) | 2024.05.03 |
#4-2 / Scanner + 더블반복문,선택문 (0) | 2024.05.03 |