在类之间传递数组...卡住了

时间:2013-04-17 23:24:19

标签: java arrays

我有三个我正在使用的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仅用于将Integers插入到数组中,然后以不同的方式操作数组。

 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;
    }
}

1 个答案:

答案 0 :(得分: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];
}

将课程视为“事物”的一种“蓝图”。字段是该“事物”的属性和数据。构造函数是该“事物”的实例化,它实际上是根据某些参数来烹饪作为类的配方的构造函数。