2013-04-17 30 views
0

我有三個java文件,我正在使用我似乎無法讓我的數組打印。我嘗試了各種各樣的東西,看起來好像我在圈子裏。我對如何處理來自不同類的數組感到困惑。預先感謝您的任何幫助。傳遞類之間的數組...卡住

這裏是主:

public class Main { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    TestInteger TI = new TestInteger(); 
    TestDouble TD = new TestDouble(); 
    Distance TDist = new Distance(); 
    mainMenu(); 
} 
    public static void mainMenu() { 
      int option; 
      Scanner sc = new Scanner(System.in); 

      while(true){ 
       System.out.println("Please choose what type of numbers will be stored in the Array"); 
       System.out.println(); 
       System.out.println("******* MAIN MENU ********"); 
       System.out.println("** 1. Integers   **"); 
       System.out.println("** 2. Double   **"); 
       System.out.println("** 3. Distance   **"); 
       System.out.println("** 4. Exit    **"); 
       System.out.println("**************************"); 

       try{ 
        option = sc.nextInt(); 

        switch (option) { 
        case 1: { 

         TestInteger.arrayMenu(); 
        } 
        case 2: { 

         TestDouble.arrayMenu(); 
        } 
        case 3: { 

         Distance.arrayMenu(); 
        } 

        case 4: { 
         System.out.println("Exiting Program"); 
         System.exit(0); 
        } 

        default: { 
         System.out.println(); 
         System.out.println("Invalid option. Please select option" + " 1 - 4"); 
         System.out.println(); 

         mainMenu(); 
        } 
        } 
       }catch(InputMismatchException e){ 
        System.out.println("Enter only 1-4"); 
        sc.nextLine(); 
       } 
      } 
     } 

} 

的SortedArray類應該持有對象的數組排序的所有方法與SortedArray使用。

import java.util.Arrays; 

    public class SortedArray implements Comparable { 

//private int[] sa; 

//public SortedArray(int i, int j) { 
    // TODO Auto-generated constructor stub 
//} 

public void constructor() { 
    final int initialSize = 5; 
    int incrementAmount = 3; 
    int top = -1; 

    Comparable [] sa = new Comparable[initialSize]; 
    //for(int i=0; i<5;++i){ 
    // SortedArray sa = new SortedArray(); 
    //} 
    top = -1; //shows the last item of the array 
} 

public int appropriatePosition(){ 
    int ap = 0; 
    return ap; 
} 

public int smallest(){ 
    int smallest = 0; 
    System.out.println("The smallest is "); 
    return smallest; 
} 

public int largest(){ 
    int largest = 0; 
    System.out.println("The largest is "); 
    return largest; 

} 

public void insert(int i){ 
    int pos = 0; 

    pos++; 
} 

public void find(){ 

} 

public void delete(){ 

} 

public Comparable[] print(){ 
    Comparable[] sa = {2,3,4,5}; 
    System.out.println(sa); 
    return sa; 
} 

public void clear(){ 
    System.out.println("Now Clearing the Array...................................."); 
    // Arrays.fill(sa, null); 

} 

public boolean full(){ 
    boolean full = false; 
    return full; 
} 

public boolean empty(){ 
    boolean empty = false; 
    return empty; 
} 

@Override 
public int compareTo(Object arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

TestInteger類僅用於將整數插入到數組中,然後以不同方式操作數組。

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

public class TestInteger implements Comparable{ 

/** 
* This is the User Interface for manipulating the sorted array 
*/ 

    static SortedArray sa = new SortedArray (5,3); 

    public static void arrayMenu() { 

     int option; 

     while(true){ 
      System.out.println(); 
      System.out.println("**** Integer Array Menu ****"); 
      System.out.println("****************************"); 
      System.out.println("** 1. Insert    **"); 
      System.out.println("** 2. Delete    **"); 
      System.out.println("** 3. Clear    **"); 
      System.out.println("** 4. Smallest   **"); 
      System.out.println("** 5. Largest    **"); 
      System.out.println("** 6. Return to Main Menu **"); 
      System.out.println("****************************"); 

      Scanner sc = new Scanner(System.in); 
     try{ 

      option = sc.nextInt(); 
      switch (option){ 
      case 1:{ 
       try{ 
        System.out.println("Type an integer to insert: "); 
        int x = sc.nextInt(); 
        int index = 0; 
        sa.insert(x); 
        sa.print(); 
       }catch(InputMismatchException e){ 
        System.out.println("Enter only integers"); 
        sc.nextLine(); 
       } 
       arrayMenu(); 
      } 
      case 2:{ 
       try{ 
        System.out.println("Type the index of the item you wish to delete:"); 
        int d = sc.nextInt(); 
       }catch(InputMismatchException e){ 
        System.out.println("Enter only integers"); 
        sc.nextLine(); 
       } 
       arrayMenu(); 
      } 
      case 3:{ 
       System.out.println("Before clearing"); 
       sa.print(); 
       sa.clear(); 
       System.out.println("After clearing"); 

       sa.print(); 
       arrayMenu(); 
      } 
      case 4:{ 
       sa.smallest(); 
       arrayMenu(); 
      } 
      case 5:{ 
       sa.largest(); 
       arrayMenu(); 
      } 
      case 6:{ 
       Main.mainMenu(); 
      } 
      default: { 
       System.out.println(); 
       System.out.println("Invalid option. Please select option 1 - 6"); 
       System.out.println(); 

       arrayMenu(); 
      } 
      } 
     }catch(InputMismatchException e){ 
      System.out.println("Enter only 1-6"); 
      sc.nextLine(); 
      } 

     } 
} 


    @Override 
    public int compareTo(Object o) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
} 
+0

確實SortedArray sa = new SortedArray();實際上做什麼?我相當肯定構造函數不能被命名爲「構造函數」... –

+0

我評論說,因爲我得到奇怪的錯誤。好吧,我會改變構造函數的方法名稱。 – Neophyte

回答

0

請注意,您的SortedArray類沒有實際狀態。 java中的東西只存在於包含它們的大括號內。我假設你的意思是你的構造函數是:

public void SortedArray(int initialSize, int incrementAmount) { 
    final int initialSize = 5; 
    int incrementAmount = 3; 
    int top = -1; 
    Comparable [] sa = new Comparable[initialSize]; 
    top = -1; //shows the last item of the array 
} 

構造函數始終共享類的名稱。您的代碼默認爲無參數的空構造函數。

但它應該是更多的東西是這樣的:

private int incrementAmount; 
private int top; 
private Comparable[] sa; 
public void SortedArray(int initialSize, int incrementAmount){ 
    this.initialSize = initialSize; 
    this.incrementAmount = incrementAmount; 
    this.top = -1 
    this.sa = new Comparable[initialSize]; 
} 

類看成是一種「藍圖」的一個「東西」。字段是「事物」的屬性和數據。一個構造函數就是這個'事物'的實例化,它實際上就是構造函數,它根據某些參數來烹飪這個類的食譜。

+0

非常感謝。當我運行程序並嘗試插入一個整數時,我得到:[Ljava.lang.Comparable; @ 5a8a0d5d當它打印出來。 – Neophyte