2011-12-02 2 views
0

숫자를 작게 배열하는 데 문제가 있습니다. 임 sortedinlist에있는 intlist의 메서드를 무시하려고, 내가 얻을 출력 0, 0,0입니다. 내가 어떻게 고칠 수 있니?상속을 사용하여 배열의 최소값에서 최대 값으로 배열 정렬 (오류 무시)

run: 
0: 100 
1: 50 
2: 200 
3: 25 

0: 0 
1: 0 
2: 0 

SortedIntList.java

public class SortedIntList extends IntList 

{ 

    public SortedIntList(int Size) 
    { 
     super(Size); 
    } 

    public void add(int value) 
    { 
    if (numElements == list.length) 
     System.out.println("Can't add, list is full"); 

    else{ 
     int empty = 0; 
     int f; 
     for(f=0;f<list.length-1;f++){ 
       if(list[f] == 0) 
      {empty = f;} 
     } 

     list[empty] = value; 

     numElements++;  


    } 
    int k = 0; int temp = 0; 
    for(int i=0;i<list.length-1;i++) 
     { 
     k = i; 
     for(int j = i+1; j < list.length; j++) 
     { 
      if(list[j] < list[k]) 
       k = j; 
     } 

     temp = list[k]; 
     list[k] = list[i]; 
     list[i] = temp; 
    } 
     } 
    } 

ListTest.java

public class ListTest { 
public static void main(String[] args) 
{ 
IntList myList = new IntList(10); 
myList.add(100); 
myList.add(50); 
myList.add(200); 
myList.add(25); 
System.out.println(myList); 

SortedIntList myList2 = new SortedIntList(10); 
myList2.add(20); 
myList2.add(100); 
myList2.add(30); 
System.out.println(myList2); 

} 
} 

IntList.java

public class IntList { 

protected int[] list; 
protected int numElements = 0; 
//------------------------------------------------------------- 
// Constructor -- creates an integer list of a given size. 
//------------------------------------------------------------- 
public IntList(int size) 
{ 
list = new int[size]; 
} 
//------------------------------------------------------------- 
// Adds an integer to the list. If the list is full, 
// prints a message and does nothing. 
//------------------------------------------------------------- 
public void add(int value) 
{ 
if (numElements == list.length) 
System.out.println("Can't add, list is full"); 
else 
{ 
list[numElements] = value; 
numElements++; 
} 
} 

//------------------------------------------------------------- 
// Returns a string containing the elements of the list with their 
// indices. 
//------------------------------------------------------------- 
public String toString() 
{ 
String returnString = ""; 
for (int i=0; i<numElements; i++) 
returnString += i + ": " + list[i] + "\n"; 
return returnString; 
} 

} 

답변

1

귀하의 문제는 당신이 말에 새 번호를 추가 할 것입니다 전체 목록. 이 코드를 참조하십시오 (약간 재 포맷 됨)

int empty = 0; 
for(int f=0;f<list.length-1;f++){ 
    if(list[f] == 0) { 
     empty = f; 
    } 
} 

list[empty] = value; 

귀하의 목록은 비어 있지만 5 개의 요소가있을 수 있습니다. 이것은 당신이 지금 목록을 인쇄하는 경우에만 삽입 된 요소는 목록의 마지막에이기 때문에 0 첫 번째 요소를 인쇄, 그러나

Check position 0 is 0. Ok, set empty to 0. 
Check position 1 is 0. Ok, set empty to 1. 
Check position 2 is 0. Ok, set empty to 2. 
Check position 3 is 0. Ok, set empty to 3. 
Check position 4 is 0. Ok, set empty to 4. 
As end of list, insert new value at "empty" position (4). 
Increase the actual length by 1. 

일어나는 것입니다.

루프에서 조기에 빠져 나오거나 목록에 몇 개의 요소가있을 수 있는지를 확인하기 위해 실제 요소 수를 확인하십시오.

-1

SortedList에서 재정의 할 때 부모 add() 메서드를 호출하는 것이 좋습니다. 재정의 된 메서드에서 이상한 논리가 있습니다. 나는 문제가 여기에 있다고 생각한다.

import java.util.Arrays; 
    import java.util.Scanner; 


    public class apples { 

     public static void main(String[] args) { 
      Scanner input = new Scanner(System.in); 
      System.out.println("Enter the number of the values"); 
      int num = input.nextInt(); 
      int a[] = new int [num]; 
      System.out.println("Enter the nambers now"); 
      for(int i=0; i<a.length; i++){ 
       a[i] = input.nextInt(); 
      } 
      Arrays.sort(a); 
     int min =a[0]; 
     System.out.println("The minmum value is "+min); 
     int max= a[a.length-1]; 
     System.out.println("The maxemum value is "+max); 
     int d = max-min; 
     System.out.println("the difrentc between the max and the min is "+ d); 

      System.out.println("The Arrays R \t"); 
      for(int g=0; g<=a.length;g++){ 
       int m = a[g]; 
       System.out.println(m); 
      } 

     } 

    } 
:
0

정렬 방법에 대한 예는
관련 문제