안녕하세요. 병합 정렬을 수행하고 있습니다. 모든 것이 정확하다고 생각합니다. merge_sort 함수의 마지막 두 줄을 사용하는 데 어려움을 겪고 있습니다 ... 마지막 줄의 두 번째 줄에는 "정수를 확인할 수 없습니다. 변수에 대해 ArrayList를 변수로 해석 할 수 없습니다. 형식이 일치하지 않습니다. ArrayList를 int []로 변환 할 수 없습니다. " 마지막 줄에 "유형 불일치가 int []에서 ArrayList로 변환 할 수 없습니다." 이 문제를 어떻게 해결할 수 있습니까? 어떤 도움이라도 대단히 감사합니다 !!ArrayList <integer>을 함수와 동일하게 설정하는 방법은 무엇입니까?
public static ArrayList<Integer> merge_sort(ArrayList<Integer> B)
{
if (B.size() <= 1)
System.out.println(B);
int midpoint = B.size()/2;
ArrayList<Integer> left = new ArrayList<Integer>(midpoint);
ArrayList<Integer> right;
if(B.size() % 2 == 0)
right = new ArrayList<Integer>(midpoint);
else
right = new ArrayList<Integer>(midpoint + 1);
int[] result = new int[B.size()];
for (int i = 0; i < midpoint; i++)
left.set(i, B.get(i));
int x = 0;
for (int j = midpoint; j < B.size(); j++)
{
if(x < right.size())
right.set(x, B.get(j));
x++;
}
left = merge_sort(left);
right = merge_sort(right);
result = merge(left, right);
return result;
}
public static ArrayList<Integer> merge(ArrayList<Integer> left, ArrayList<Integer> right)
{
int lengthResult = left.size() + right.size();
ArrayList<Integer> result = new ArrayList<Integer>(lengthResult);
int indexL = 0;
int indexR = 0;
int indexRes = 0;
while (indexL < left.size() || indexR < right.size())
{
if(indexL < left.size() && indexR < right.size())
{
if (left.get(indexL) <= right.get(indexR))
{
result.set(indexRes, left.get(indexL));
indexL++;
indexRes++;
}
else
{
result.set(indexRes, right.get(indexR));
indexR++;
indexRes++;
}
}
else if (indexL < left.size())
{
result.set(indexRes, left.get(indexL));
indexL++;
indexRes++;
}
else if (indexR < right.size())
{
result.set(indexRes, right.get(indexR));
indexR++;
indexRes++;
}
}
return result;
}
int[] result
그래서 I 제거 정의된다 INT [] = 결과 새로운 INT [B.size()]; 및 바꾸기 결과 = 병합 (왼쪽, 오른쪽); 리스트 용@David Camacho'List'는 java.util 패키지 (awt가 아님)에 있어야합니다. 가져 오기 선언을 확인하십시오. – Alex
nevermind 라이브러리 파일을 추가했습니다. import java.util.List; 하지만 지금은 마지막 줄 I merge_sort의 (귀국일 결과에) 나는 형식 불일치은'리스트을 반환 –
Isaac