检查数组是否已在Java中创建

检查数组是否已在Java中创建,java,arrays,Java,Arrays,您可以使用可以从主类检查的全局数组变量来检查是否已经创建了数组 添加此行int数组[]将CreateArray类作为,并替换int-array[]=new-int[num]带有数组=新整数[num]以便它引用全局变量 然后,从main/Lab3类中,您可以简单地使用if(myObj.array==null)检查数组是否尚未创建,或者使用if(myObj.array!=null)检查数组是否已创建 注意,您还需要将以下代码放置在do{}while()loopCreateArray myObj=ne

您可以使用可以从主类检查的全局数组变量来检查是否已经创建了数组

添加此行
int数组[]
将CreateArray类作为,并替换
int-array[]=new-int[num]带有
数组=新整数[num]以便它引用全局变量

然后,从main/Lab3类中,您可以简单地使用
if(myObj.array==null)
检查数组是否尚未创建,或者使用
if(myObj.array!=null)
检查数组是否已创建

注意,您还需要将以下代码放置在
do{}while()
loop
CreateArray myObj=newcreatearray()之外否则它将在每个循环中创建一个新对象,您将无法保留输入

完整的代码可能如下所示(还有一些其他更改以使其更有意义):


请修复代码的缩进。如果您使用的是IDE,您可以让它为您格式化代码,很可能是通过键入Ctrl-F。很抱歉,这对编程来说是一种新的方式。数组需要是这样的
public static int array[]
,以便您可以从主类访问它,并检查它是否已经创建
If(choice.equals(“1”)&&myObj.array!=null){System.out.print(“已创建数组,请转到其他选项!”);}
如何从中生成全局变量?我真的很抱歉打扰你,我是新来的。非常感谢你,帮了我很多忙。感谢上帝保佑你
class CreateArray{ 
Scanner input = new Scanner(System.in);

public void Create(){
System.out.print("How many numbers numbers do you want to enter? Minimum of 5 and a maximum of 20 only: ");
    int num = input.nextInt();
    if (num >=5 || num >20){
        int array[] = new int[num];
        System.out.println("Enter the numbers you want: ");
        if(array.length != 0){
        for(int i = 0 ; i<array.length; i++){
            array[i] = input.nextInt();
        
        }
    }
        System.out.print("Your array of numbers are: ");
        for(int i = 0 ; i<array.length; i++){
            System.out.print(array[i] + " ");
        }
    }
    else{
        System.out.print("Please input a the right numbers of array");
    
    }   }}
public class Lab3

{public static void main(String[] args){
    
    Scanner ans = new Scanner(System.in);
    String choice;
    String choices;
    do{
        System.out.println("[1] Create Array");
        System.out.println("[2] Insert Element");
        System.out.println("[3] Search");
        System.out.println("[4] Display");
        System.out.println("[5] Delete");
        System.out.println("[0] Stop");
        System.out.print("\nEnter Choice: ");
        choice = ans.nextLine();
        if(choice.equals("1")){
            CreateArray myObj = new CreateArray();
            myObj.Create();
    }
    else{
        System.out.print("Array has been created please procedd to other options!");
    }
    System.out.println();
    System.out.print("Do you want to continue? : ");
    choices =ans.nextLine();
}
while(!choices.equals("-1") || !choices.equals("-1"));
}}
class CreateArray {

    Scanner input = new Scanner(System.in);
    //place a global variable here
    int array[];

    public void Create() {
        System.out.print("How many numbers numbers do you want to enter? Minimum of 5 and a maximum of 20 only: ");
        int num = input.nextInt();
        //Fix this line as shown by Scary Wombat in comments:
        if (num <= 5 || num <= 20) {
            //Initialize the array here (This will make it a non null value)
            array = new int[num];
            System.out.println("Enter the numbers you want: ");
            if (array.length != 0) {
                for (int i = 0; i < array.length; i++) {
                    array[i] = input.nextInt();
                }
            }
            System.out.print("Your array of numbers are: ");
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + " ");
            }
        } else {
            System.out.print("Please input a the right numbers of array");
        }
    }
}
public class Lab3 {

    public static void main(String[] args) {

        Scanner ans = new Scanner(System.in);
        String choice;
        String choices;
        //Place the CreateArray object here so that it can be re-used between selecting options, otherwise you will lose all progress
        CreateArray myObj = new CreateArray();
        do {
            System.out.println("[1] Create Array");
            System.out.println("[2] Insert Element");
            System.out.println("[3] Search");
            System.out.println("[4] Display");
            System.out.println("[5] Delete");
            System.out.println("[0] Stop");
            System.out.print("\nEnter Choice: ");
            choice = ans.nextLine();
            //Only call this method if it has not been created yet
            if (choice.equals("1") && myObj.array == null) {
                myObj.Create();
            }
            //If another option is chosen but the array has not been created, then give an error message
            else if (myObj.array == null) {
                System.out.print("Array has not been created yet, please choose option 1.");
            }
            else if (choice.equals("1")) {
                System.out.print("THe array has already been created, please choose another option.");
            } 
            //Place all other value checkes after the above checks:
            else if (choice.equals("2")) {
                System.out.print("This opetion is not yet supported.");
            } else if (choice.equals("3")) {
                System.out.print("This opetion is not yet supported.");
            } else if (choice.equals("4")) {
                System.out.print("This opetion is not yet supported.");
            } else if (choice.equals("5")) {
                System.out.print("This opetion is not yet supported.");
            } else if (choice.equals("0")) {
                System.out.print("This opetion is not yet supported.");
                break;
            } else {
                System.out.print("Invalid option.");
            }
            System.out.println();
            System.out.print("Do you want to continue? : ");
            //What does this do?
            choices = ans.nextLine();
        } while (!choices.equals("-1"));
    }
}