2012-12-04 2 views
2

개체가 들어있는 ArrayList가 있습니다. 내가 원했던 /해야 할 일은 ArrayList 내에서 특정 Object를 검색하고 그 중 하나를 찾으면 값을 변경하는 것입니다. I.E. 필자는 팀의 이름이 들어있는 필라델피아 이글스 (Philadelphia Eagles) 팀과 총 자루 점수를받은 축구 선수 (Football Players) 팀을 보유하고 있으며 모든 선수를 이름, 성, 경기 및 위치별로 나열합니다. 특정 플레이어가 채드 홀을 찾고 있는데 그를 이글스에서 빼내 뉴 잉글랜드 패트리어트와 거래하려고합니다. 지금까지 ArrayList의 반복이 있었지만 코드가 플레이어를 찾지 못하고 종료되었습니다.arraylist에서 개체 검색 및 변경

case 6: 
      String name = ""; 
      System.out.print("Who would you like to trade? "); 
      name = kb.nextLine(); 
      tradePlayer(nflTeams, name); 

public static void tradePlayer(ArrayList<Team> nflTeams, String name) 
    { 
    Scanner kb = new Scanner(System.in); 
    String teamName = ""; 
    System.out.print("What team do you want to trade this player to?(Team Acronym) " ); 
    teamName = kb.nextLine(); 

    for(Team t: nflTeams){ 
     if (nflTeams.contains(name.toLowerCase())) 
         t.setName(teamName); 
       } 
    System.out.println("Cannot find " + name); 
    } 

이름, 성, 팀, 위치에 의한 Player 클래스 참여 건설 플레이어를했다. 내가 바꾸고 싶은 것은 팀입니다. Team 클래스에는 팀 이름을 설정하는 setName(String)이 있습니다.

모든 의견이나 생각은 감사하겠습니다.

+1

는 사기 보인다 내지 .. –

답변

2

if (nflTeams.contains(name.toLowerCase()))

? 귀하의 목록 nflTeams에는 Team 개체의 이름이 포함되어 있지 않습니다.
이것은 작동하지 않습니다. 당신은 name를 통해 Team에 액세스해야하는 경우
다음 대신 반복자의

0

당신은 팀 플레이어의 목록을 얻을에 의해 거기에서 삭제해야 지정된 같은 name : 당신이 뭘 하려는지

String teamToAddNewPlayer = "New England Patriots"; 
String deletedPlayer = null; 
for(Team t: nflTeams){ 
     List<String> players = t.getPlayers(); 
     if (players.contains(name)){ 
      deletedPlayer = players.remove(name); 
     } 
     if (t.getName().equals(teamToAddNewPlayer)) 
      players.add(name); 
} 
if (deletedPlayer == null) 
//nothing was deleted 
else 
//player was deleted 
0

사용 당신이 시도하고있는 무슨에 당신에게 더 많은 제어를 제공합니다 Map<String,Team>를 사용합니다.

import java.util.*; 

class IteratorDemo { 


public static void main(String args[]) { 
     // Create an array list 
     ArrayList al = new ArrayList(); 
     // add elements to the array list 
     al.add("C"); 
     al.add("A"); 
     al.add("E"); 
     al.add("B"); 
     al.add("D"); 
     al.add("F"); 

    // Use iterator to display contents of al 
    System.out.print("Original contents of al: "); 
    Iterator itr = al.iterator(); 
    while(itr.hasNext()) { 
    Object element = itr.next(); 
    System.out.print(element + " "); 
    } 
    System.out.println(); 

    // Modify objects being iterated 
    ListIterator litr = al.listIterator(); 
    while(litr.hasNext()) { 
    Object element = litr.next(); 
    litr.set(element + "+"); 
    } 
    System.out.print("Modified contents of al: "); 
    itr = al.iterator(); 
    while(itr.hasNext()) { 
    Object element = itr.next(); 
    System.out.print(element + " "); 
    } 
    System.out.println(); 

    // Now, display the list backwards 
    System.out.print("Modified list backwards: "); 
    while(litr.hasPrevious()) { 
    Object element = litr.previous(); 
    System.out.print(element + " "); 
    } 
    System.out.println(); 
} 
} 

이 예 MMN가 포함 source