Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在类之间传递数组。。。卡住了_Java_Arrays - Fatal编程技术网

Java 在类之间传递数组。。。卡住了

Java 在类之间传递数组。。。卡住了,java,arrays,Java,Arrays,我有三个正在使用的java文件,我似乎无法打印我的数组。我尝试过各种各样的事情,似乎我在兜圈子。我对如何使用来自不同类的数组感到困惑。提前感谢您的帮助 主要内容如下: public class Main { /** * @param args */ public static void main(String[] args) { TestInteger TI = new TestInteger(); TestDouble TD = new TestDouble();

我有三个正在使用的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();
                }
            }
        }

}
SorterDarray类应该包含一个已排序的对象数组,以及与SorterDarray一起使用的所有方法

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

请注意,您的SorterDarray类没有实际状态。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];
}

把类看作是一种“事物”的“蓝图”。字段是那个“东西”的属性和数据。构造函数是该“东西”的实例化,它实际上是根据某些参数烹饪类配方的构造函数。

SorterDarray sa=new SorterDarray();真的做了什么?我相当确定构造函数不能被命名为“构造函数”…我对此进行了评论,因为我遇到了奇怪的错误。好的,我将更改构造函数方法的名称。非常感谢。当我运行程序并尝试插入一个整数时,我得到:[Ljava.lang.Comparable;@5a8a0d5d,当它打印出来时。
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];
}