int[] ref = new int[5];
1. int[] arr = new int[] {1, 2, 3};
2. int[] arr = {1, 2, 3};
int[] ar1 = new int[5];
public static void main(String[] args) {
Box2[] ar = new Box2[5];
ar[0] = new Box2(101, "Coffee");
ar[1] = new Box2(202, "Computer");
ar[2] = new Box2(303, "Apple");
ar[3] = new Box2(404, "Dress");
ar[4] = new Box2(505, "Fairy-tale book");
for(Box2 e: ar) {
if(e.getBoxNum() == 505)
System.out.println(e);
}
}
======출력==========
Fairy-tale book
class Box2{
private int num;
private String str;
public Box2() {}
public Box2(int num, String str){
this.num = num;
this.str = str;
}
public int getBoxNum() {
return num;
}
public String toString() {
return str;
}
}
public class arrPrac {
public static void main(String[] args) {
Box2[] ar = new Box2[5];
ar[0] = new Box2(101, "Coffee");
ar[1] = new Box2(202, "Computer");
ar[2] = new Box2(303, "Apple");
ar[3] = new Box2(404, "Dress");
ar[4] = new Box2(505, "Fairy-tale book");
for(Box2 e: ar) {
if(e.getBoxNum() == 505)
System.out.println(e);
}
}
}
코드 옆자리 친구꺼 훔쳐왔습니다. (당당)
String[] sr = new String[7];
sr[0] = new String("Java");
sr[1] = new String("System");
sr[2] = new String("Compiler");
sr[3] = new String("Park");
sr[4] = new String("Tree");
sr[5] = new String("Dinner");
sr[6] = new String("Brunch Cafe");
public class StringArray {
public static void main(String[] args) {
// 1번 방법
int cnum = 0;
for(int i = 0; i < sr.length; i++)
cnum += sr[i].length();
System.out.println("총 문자의 수 : " + cnum);
}
}
public class StringArray {
public static void main(String[] args) {
// 2번 방법 (EnhancedFor문)
int cnum = 0;
for (String string : sr) {
cnum += string.length();
}
System.out.println("총 문자의 수 : " + cnum);
}
}
public class Lotto {
public static void main(String[] args) {
int[] lottoArr = new int[6];
for(int i=0; i < lottoArr.length; i++) {
lottoArr[i] = (int)(Math.random() *45) +1;
// 1부터 45까지 랜덤
// 중복 제거 로직
for(int j=0; j < i; j++) {
if(lottoArr[i] == lottoArr[j]) {
i--;
break;
}
}
}
for (int num : lottoArr) {
System.out.println(num);
}
}
}