2014-03-06 2 views
0

이러한 ArrayList를 병합하려고합니다. list3이 마지막 것입니다. 모든 것이 잘 작동하지만 마지막 번호 인 400은 최종 배열에 나타나지 않습니다. 나는 왜 또는 무엇을해야하는지 전혀 모른다. ArrayList를 정렬하려고 노력 중입니다. 표시되지 않는 마지막 번호를 제외하고 거의 완료되었습니다.Java의 ArrayList와 병합 정렬

import java.util.ArrayList; 
public class TextLab12st 
{ 
    public static void main(String args[]) 
{ 
    int jsaList1[] = {101, 105, 115, 125, 145, 165, 175, 185, 195, 225, 235, 275, 305, 315, 325, 335, 345, 355, 375, 385}; 
    int jsaList2[] = {110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 270, 280, 320, 350, 400}; 

    Array list1 = new Array(jsaList1,"List #1"); 
    Array list2 = new Array(jsaList2,"List #2"); 
    Array list3 = new Array("Merged List"); 

    list3.merge(list1,list2,list3); 

    list1.display(); 
    list2.display(); 
    list3.display(); 

} 

} 

class Array 
{ 
private ArrayList<Integer> list; 
private int size; 
private String listName; 

public Array(String ln) 
{ 
    list = new ArrayList<Integer>(); 
    size = 0; 
    listName = ln; 
} 

public Array(int[] jsArray, String ln) 
{ 
    list = new ArrayList<Integer>(); 
    size = jsArray.length; 
    listName = ln; 
    for (int j = 0; j < size; j++) 
     list.add(new Integer(jsArray[j])); 
} 

public void display() 
{ 
    System.out.println("\n" + listName + ":\n"); 
    System.out.println(list + "\n"); 
} 

public void merge(Array that, Array theOther, Array result) 
{ 
    { 
     // Merge both halves into the result array 
     // Next element to consider in the first array 
     int iFirst = 0; 
     // Next element to consider in the second array 
     int iSecond = 0; 

     // Next open position in the result 
     int j = 0; 
     // As long as neither iFirst nor iSecond is past the end, move the 
     // smaller element into the result. 
     while (iFirst < that.size && iSecond < theOther.size) 
     { 
      if (that.list.get(iFirst) < theOther.list.get(iSecond)) 
      { 
       result.list.add(that.list.get(iFirst)); 
       iFirst++; 
      } 
      else 
      { 
       result.list.add(theOther.list.get(iSecond)); 
       iSecond++; 
      } 
      j++; 
     } 

    } 

} 

} 두 배열을 통해 실행 루프 후

+0

collectio 뭐가 잘못 ns.sort()? – hd1

답변

4

:

당신이 배열 중 하나에 남아 거기에 아무것도 경우의 끝에 도달 한 후에 볼 필요가
while (iFirst < that.size && iSecond < theOther.size) 
    { 
     if (that.list.get(iFirst) < theOther.list.get(iSecond)) 
     { 
      result.list.add(that.list.get(iFirst)); 
      iFirst++; 
     } 
     else 
     { 
      result.list.add(theOther.list.get(iSecond)); 
      iSecond++; 
     } 
     j++; 
    } 

기타 :

if (iFirst < that,size) { 
    //copy everything remaining in that to output 
} else if (iSecond < theOther.size) { 
    //copy everything from theOther to output 
} 
+0

고마워, 나는 단서가 없었다. 어떻게 모든 것을 정확히 복사합니까? –

+0

@ima_mango - System.arrayCopy() 살펴보기 – radai

1
// this condition stops when you reach the end of either list 
// you need to continue until you reach the end of both lists 
while (iFirst < that.size && iSecond < theOther.size)