2017-12-31 90 views
2

나는 누구의 출력으로 제공하는 ArrayList를 가지고 : 어느 경기 때전체 ArrayList의 요소를 삭제

[, bd33b056-7a24-490f-a4bb-88cb2687facb%1514759804437%New York, USA%Florida, USA%2018-01-01%2018-01-10%UM-66%3050.0, bd33b056-7a24-490f-a4bb-88cb2687facb%1514759837907%New York, USA%California, USA%2018-01-01%2018-01-10%UM-66%8770.0] 

가 지금은, 매개 변수로 문자열 ID를 가지고하는 방법을 만드는 오전를 이드의 예약은 그 색인을 제거 할 것입니다. id가 처음 % 다음에 해당 예약의 색인을 찾을 수있는 방법이 있습니까? 당신이 ID는 처음에 %을 가지고 요소를 제거하려는 경우

data.removeIf(e -> e.contains(id)); 

과 끝 : 여기 방법

public static void removeElement(String id) throws FileNotFoundException, IOException{ 
    BufferedReader b = new BufferedReader(new FileReader("Booking.dat")); 
    String d = b.readLine(); 
    String[] allB = d.split("£"); 
    ArrayList<String> data = new ArrayList<String>(Arrays.asList(allB)); 
    data.remove(id);// need to have specific index of id inside the full arraylist 
    System.out.println(data); 
} 
+0

당신은 너무 ArrayList에 대한 제공된 샘플 데이터 주어진 각 요소 –

+0

당신은 키가 할 수있는지도 느릅 나무를 사용할 수 있습니다. 당신은'removeElement' 메쏘드에 대한 몇 가지 샘플 입력을 제공 할 수 있고 엘리먼트를 제거한 후에 ArrayList가 가질 것으로 기대되는 것을 기술 할 수 있습니까? –

+0

어쩌면 그것은 나뿐이지만,이 질문은 [XY 문제] (http://xyproblem.info/)라는 느낌입니다. –

답변

1

당신은 removeIf에 지정된 ID를 포함하는 요소를 제거 할 수있다 다음을 할 수 있습니다 :

data.removeIf(e -> e.contains("%"+id+"%")); 
+0

감사합니다 !! 오랫동안 이것을 찾고 있었다 –

+0

@ JordanRanen probs, 해결책은 당신을 도왔다. –

1

나는 왜 당신이 색인을 가지고 있다고 주장하는지 모르겠다. ficient,하지만이 방법은 요청에 따라 인덱스를 가져옵니다하고 해당 요소 제거 :

public static void removeElement(String id) { 
    BufferedReader b = new BufferedReader(new FileReader("Booking.dat")); 
    String d = b.readLine(); 
    String[] allB = d.split("£"); 
    ArrayList<String> data = new ArrayList<String>(Arrays.asList(allB)); 

    // Variable to save the index to. Set to -1 in case the index does not exist. 
    int index = -1; 
    for (int i = 0; i < data.size(); i++) { // Iterate through data 
     // Check if this index contains the id 
     if (data.get(i).contains(id)) { 
      index = i; // If it matches save the index and break 
      break; 
     } 
    } 
    if (index == -1) // If the index was never saved, return. 
     return; 

    data.remove(index); 
    System.out.println(data); 
} 
관련 문제