2017-10-17 3 views
-3

두 개의 배열 목록을 뺍니다. 두 arraylists는 다음과 같은 데이터2 arraylists 빼기

[1, 1, 5, 1, 1] 

나는이 작업을 수행하기 위해 아파치 라이브러리를 사용하고 있습니다 포함되어 있습니다.

List resultList = ListUtils.subtract(sections, sections); 

작업이 완료하지만 난이 일에 대해 갈 것이라고 어떻게

[0, 0, 0, 0, 0] 

수 그것을 필요로 할 때 내 결과는 다음과

[] 

입니까?

+1

는 힌트 : 당신이 생각하지 않습니다. http://adownvotedbecau.se/noresearch/ – Oleg

+0

'section.replaceAll (e -> 0); ' – shmosel

+1

[Java에서 두 목록/배열의 값을 빼는 방법] 가능한 복제본 (https://stackoverflow.com/) 질문/40644999/how-to-subtract-of-two-lists-in-java) – azurefrog

답변

0

왜 for 루프를 사용하여 명시 적으로하지 않습니까? 당신은 여기 set 기능에 좀 더 많은 정보를 볼 수 있습니다

// Initialize both lists 
List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(1, 1, 5, 1, 1)); 
List<Integer> list2 = new ArrayList<Integer>(Arrays.asList(1, 1, 5, 1, 1)); 

for(int i=0; i < list1.size(); ++i){ 
    // This is doing the subtraction and assigning the values to 
    // list1. If you need a new list, declare it and proceed similarly 
    list1.set(i, list1.get(i) - list2.get(i)); 
} 
System.out.println(list1); // output: [0, 0, 0, 0, 0] 

: 당신은`ListUtils.subtract`의 문서를보고 시작해야 https://www.tutorialspoint.com/java/util/arraylist_set.htm