2013-04-22 5 views
0

다음 코드에서 배열 크기가 20보다 큰 경우 배열에서 20 이후에 아무 것도 제거하려고합니다. 내 루프에서, 나는 userinput.remove (20 + i); 그러나 심볼 제거를 찾을 수 없다는 것을 알고 있습니까? error.add 자체가 실제로 작동하는 경우 왜 이렇게하는지 잘 모르겠습니다.배열 오류에서 요소 제거

UserInput 사용자는 당신은 remove 배열을 호출 할 수 없습니다 이전 코드

public static void checknames(String[] userinput){ 

ArrayList<String> error = new ArrayList<String>(); 


    if(userinput.length > 20){ 

     for(int i=0; i<userinput.length - 20; i++){ 
      error.add(userinput[20 + i]); 
      userinput.remove(20 + i);} 
      JOptionPane.showMessageDialog(null, "You can only enter up to 20 
      employees. \n The following employees exceed this limit." + error); 

      } 
     } 

답변

0

에서 정의된다. 배열의 크기는 변경할 수 없습니다. 하지만 당신은 null에 그 요소를 설정할 수 있습니다 :

userinput[20 + i] = null; 
0
userinput.remove(20 + i); 

userinputString[]의 배열입니다. 어레이에 사용할 수있는 방법은 remove(..)이 아닙니다.

는 20 (또는)보다 큰 인덱스에 대해 null에 값을 설정 한 경우에만 처음 20 개 요소와 새로운 String 배열을 만들고 userinput를 폐기 할 필요가있을 수 있습니다.

3

오류는 정확합니다. 배열에 대해서는 remove 방법이 없습니다. 당신은해야 하나 다음 ArrayList 당신이 error에 사용한 같은

  • 가 대신 List를 사용합니다.
  • 1 개의 요소가 더 짧은 새 배열을 만들고 제거하려는 요소를 제외한 모든 요소를 ​​복사하십시오.
0

이 시도 :

public static void checknames(String[] userinput) { 

    List<String> error = new ArrayList<String>(); 

     for(int i=20; i<userinput.length; i++) { 
      error.add(userinput[i]); 
      userinput[i] = null; 
     } 
     JOptionPane.showMessageDialog(null, "You can only enter up to 20 
      employees. \n The following employees exceed this limit." + error); 

} 

그냥 몇 가지 작은 변화를. 왼쪽에는 항상 ArrayList (List<...>)과 같이 입력해야합니다. 또한 if 문을 제거하고 루프가 약간 변경되었으므로 필요하지 않습니다. 다른 모든 사람들이 언급했듯이 .remove(...)은 어레이에서 작동하지 않습니다.

0

당신은 문자열 [], 기존 API 메소드에 "더러운 일"을 위임 할 수있는 즉, Arrays.copyOfRange(Object[] src, int from, int to)


짧은, 콘도, (에 Compilable), 올바른 예를 유지 주장하는 경우 :

import java.util.Arrays; 

public class R { 
    public static String[] trimEmployees(String[] employees, int maxSize) { 
     return Arrays.copyOfRange(employees, 0, maxSize); 
    } 

    public static void main(String[] args) { 
     String[] employees = new String[] { "Jennifer", "Paul", "Tori", 
       "Zulema", "Donald", "Aleshia", "Melisa", "Angelika", "Elda", 
       "Elenor", "Kimber", "Eusebia", "Mike", "Karyn", "Marinda", 
       "Titus", "Miki", "Alise", "Liane", "Suzanne", "Dorothy" }; 
     int max = 20; 

     System.out.println(String.format("Input employees (len=%d): %s ", 
       employees.length, Arrays.toString(employees))); 
     if (employees.length > max) { 
      employees = trimEmployees(employees, max); 
      System.out.println(String.format("Trimmed employees (len=%d): %s", 
        employees.length, Arrays.toString(employees))); 
     } 
    } 
} 

인쇄 :

Input employees (len=21): [Jennifer, Paul, Tori, Zulema, Donald, Aleshia, Melisa, Angelika, Elda, Elenor, Kimber, Eusebia, Mike, Karyn, Marinda, Titus, Miki, Alise, Liane, Suzanne, Dorothy] 
Trimmed employees (len=20): [Jennifer, Paul, Tori, Zulema, Donald, Aleshia, Melisa, Angelika, Elda, Elenor, Kimber, Eusebia, Mike, Karyn, Marinda, Titus, Miki, Alise, Liane, Suzanne]