ex1. 기본형타입[] 변수명;
ex2. 기본형타입 변수명[];
기본형타입[] 변수명 = new 기본형타입[배열의크기];
변수명[index값] = 값;
기본형타입[] 변수명 = new 기본형타입[]{ 값1, 값2, .... };
기본형타입[] 변수명 = {값1, 값2, 값3.... };
public class Array05 {
public static void main(String[] args) {
ItemForArray[] array1 = new ItemForArray[3];
array1[0] = new ItemForArray(500, "사과");
array1[1] = new ItemForArray(300, "바나나");
array1[2] = new ItemForArray(900, "수박");
ItemForArray[] array2 = new ItemForArray[]{new ItemForArray(500, "사과"), new ItemForArray(300, "바나나"), new ItemForArray(900, "수박")};
ItemForArray[] array3 = {new ItemForArray(500, "사과"), new ItemForArray(300, "바나나"), new ItemForArray(900, "수박")};
System.out.println(array1[0].getName());
System.out.println(array1[0].getPrice());
System.out.println(array1[1].getName());
System.out.println(array1[1].getPrice());
System.out.println(array1[2].getName());
System.out.println(array1[2].getPrice());
}
}
타입[][] 변수명 = new 타입[행의수][열의수];
변수명[행인덱스][열인덱스] = 값;
public class Array09 {
public static void main(String[] args) {
int[][] array = {{0,1,2}, {3,4,5}};
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}
타입[][] 변수명 = new 타입[행의수][];
변수명[행의인덱스] = new 타입[열의수];
public class Array10 {
public static void main(String[] args) {
int[][] array = new int[2][];
array[0] = new int[2];
array[1] = new int[3];
array[0][0] = 0;
array[0][1] = 1;
array[1][0] = 2;
array[1][1] = 3;
array[1][2] = 4;
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}
public class Array12 {
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
for(int i : array){
System.out.println(i);
}
}
}
// i는 index가 아닌 array안의 값
public class EmptyCommandLineArgumentExam {
public static void main(String[] args){
System.out.println(args.length);
}
}
public class UnlimitiedArgumentsExam {
public static void main(String[] args){
System.out.println(sum(5,10));
System.out.println(sum(1,2,4,2));
System.out.println(sum(3,1,2,3,4,1));
}
public static int sum(int... args){
System.out.println("print1 메소드 - args 길이 : " + args.length);
int sum = 0;
for(int i = 0; i < args.length; i++){
sum += args[i];
}
return sum;
}
}
//print1 메소드 - args 길이 : 2
//15
//print1 메소드 - args 길이 : 4
//9
//print1 메소드 - args 길이 : 6
//14
public class Book {
String title;
int price;
public Book() { } // 기본생성자
public Book(String title, int price) { // 매개변수를 가진 생성자
this.title = title;
this.price = price;
}
}