-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
arraylist와 강화for문을 잘 이해를 못해서 다음주 화요일까지 코드를 다시 짜와도 될까요? #2
base: master
Are you sure you want to change the base?
Conversation
너무 수고하셨습니다! |
String balls = n.nextLine(); | ||
ball = balls.split(","); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String으로 받고 split() 을 사용할 필요 없이
ball = n.nextLine().split(",")
과 같이 바로 문자열 배열에 입력받을 수 있습니다.
Attacker[] attackerArray = {new Attacker("attacker1"), new Attacker("attacker2")}; | ||
int flag = 0; | ||
ArrayList<Attacker> attackers = Arrays.stream(attackerArray) | ||
.collect(Collectors.toCollection(ArrayList::new)); | ||
|
||
Goalkeeper[] goalkeeperArray = {new Goalkeeper("goalkeeper")}; | ||
|
||
ArrayList<Goalkeeper> goalkeepers = Arrays.stream(goalkeeperArray) | ||
.collect(Collectors.toCollection(ArrayList::new)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attacker와 goalkeeper를 클래스로 선언한 후, 객체 배열을 만드시고 그 객체배열을 다시 ArrayList로 바꾸셨는데,
ArrayList<String> attacker1;
이런식으로 클래스가 아닌 공의 값을 담는 ArrayList로 그냥 각각 선언하시면 됩니다.
for(int i = 0; i < 5; i++) { | ||
flag=0; | ||
for(int j=0; j<4; j++){ | ||
if(attackerArray[0].ball[i].equals(goalkeeperArray[0].ball[j])){ | ||
flag = 1; | ||
break; | ||
} | ||
} | ||
if(flag==1){ | ||
attackerArray[0].count--; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flag 변수가 존재하는 이유를 모르겠네요. 골키퍼와 공이 겹치면 flag를 바꿀 필요 없이 그냥 바로 공격수의 점수를 차감하는게 훨씬 간결합니다.
for(int j=0; j<4; j++){ | ||
if(attackerArray[1].ball[i].equals(goalkeeperArray[0].ball[j])){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이중 for문을 사용할 필요가 없습니다.
ArrayList.contains() 메서드에 대해 알아보세요.
attackerArray[0].count--; | ||
} | ||
} | ||
for(int i = 0; i < 5; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
공격수의 점수를 계산하는 곳에도
for(String i : attackerArray[1].ball)
이런식으로 for each문법을 사용하면 훨씬 더 간결해집니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
골키퍼 공 목록을 ArrayList가 아닌 배열로 선언해버려서 stream.toList()
를 사용해야겠네요.
import java.util.stream.Collectors; | ||
|
||
class Attacker { | ||
private static Scanner n = new Scanner(System.in); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java에서 외부 리소스를 이용하는 경우, 항상 리소스를 모두 사용한 이후에는 close()를 해주는 것이 중요합니다.
객체 안에 private로 선언한 경우에는 따로 닫는 메서드를 만들어서 close 해주어야 합니다.
No description provided.