如何创建所有方法都可访问的 public 数组,但让用户输入确定它的大小?

How to create public array accessible by all methods, but have user input determine the size of it?

我有多个数组,其大小需要由用户输入来确定。这些数组应该可以在 main 方法和 stepTwo() 方法中访问。但是,我被卡住了。用户输入直到 main 方法才出现,但是如果我在 main 方法中声明数组,那么我就无法在 stepTwo() 方法中访问数组。我不想像我之前尝试的那样将数组作为参数传递给 stepTwo(),但出现了多个错误。有什么建议么?完整代码见下方:

    public class AssignmentIII
    {       
    public static int numProcesses; // Represents the number of processes
    public static int numResources; // Represents the number of different types of resources

    public static int[] available = new int[numResources]; // Create an emptry matrix for available processes
    public static int[][] allocation = new int[numProcesses][numResources]; // Create an empty allocation matrix nxm
    public static int[][] request = new int[numProcesses][numResources]; // Create an empty request matrix nxm
    public static int[] work = new int[numResources]; // Create an empty work matrix
    public static Boolean[] finish = new Boolean[numProcesses]; // Create an empty finish matrix

    public static void main(String[] args) throws FileNotFoundException
    {
        try
        {
            Scanner scan = new Scanner(System.in);
            Scanner fileScan = new Scanner(new File("input1.txt")); // Create file scanner

            System.out.println("Please enter the total number of processes: ");
            numProcesses = scan.nextInt();
            System.out.println("Please enter the number of different types of resources: ");
            numResources = scan.nextInt();

            // Initialize the available matrix
            for(int i = 0; i < numResources; i++)
            available[i]=fileScan.nextInt();

            // Initialize the allocation matrix
            for(int j = 0; j < numProcesses; j++)
                for(int k = 0; k < numResources; k++)
                    allocation[j][k]=fileScan.nextInt();

            // Initialize the request matrix
            for(int m = 0; m < numProcesses; m++)
                for(int n = 0; n < numResources; n++)
                    request[m][n]=fileScan.nextInt();

            // Print allocation matrix
            System.out.println();
            System.out.println("Allocated");
            for(int i = 0; i < numResources; i++)
            {
                System.out.print("\tR" + i);
            }
            System.out.println();
            for(int j = 0; j < numProcesses; j++)
            {
                System.out.print("P" + j);
                for(int k = 0; k < numResources; k++)
                {
                    System.out.print("\t" + allocation[j][k]);
                }
                System.out.println();
            }

            // Print available matrix
            System.out.println();
            System.out.println("Available");
            for(int i = 0; i < numResources; i++)
            {
                System.out.print("R" + i + "\t");
            }
            System.out.println();
            for(int i = 0; i < numResources; i++)
                System.out.print(available[i] + "\t");
            System.out.println();


            // Print request matrix
            System.out.println();
            System.out.println("Requested");
            for(int i = 0; i < numResources; i++)
                {
                    System.out.print("\tR" + i);
                }
            System.out.println();

            for(int m = 0; m < numProcesses; m++)
            {
                System.out.print("P" + m);
                for(int n = 0; n < numResources; n++)
                {
                    System.out.print("\t" + request[m][n]);
                }
                System.out.println();
            }
            System.out.println();

            // Begin deadlock detection algorithm               
            for(int i = 0; i < numResources; i++) // Intialize Work := Available
                work[i]=available[i];

            for(int j = 0; j < numProcesses; j++) // Check for Allocation != 0 and initialize Finish accordingly
            {
                int sumAllocation = 0;
                for(int i = 0; i < numResources; i++)
                {
                    sumAllocation += allocation[j][i];
                }

                if (sumAllocation != 0)
                    finish[j] = false;
                else finish[j] = true;
            }

            stepTwo();

            }
            catch(FileNotFoundException ex)
            {
            System.out.println("An error has occured. The file cannot be found.");
            }
    }

    public static void stepTwo()
    {
        // Step 2
        // Find an index i where Finish[i] = false & Request[i] <= Work         
        for(int i = 0; i < numProcesses; i++)
        {
            int sumRequests = 0;
            int sumWork = 0;

            // Sum the Request and Work vectors
            for(int k = 0; k < numResources; k++)
            {   
                sumRequests += request[i][k];
                sumWork += work[k];
            }

            if (finish[i] == false && sumRequests <= sumWork)
            {
                finish[i] = true;
                for(int m = 0; m < numResources; m++)
                {
                    work[m] = work[m] + allocation[i][m];
                }

                stepTwo();
            }
            else if (finish[i] == false)
            // Step 4: Print which processes are in a deadlock state
            // Print using P0, P1, ... , Pn format
                System.out.println("P" + i + " is in a deadlock state.");                           
        }
    }
 }

像您一样声明数组 - "above main",但在读取适当大小后对其进行初始化。

声明:

public static int[] available;

初始化:

available = new int[numResources];

你必须将数组初始化到主块中,就像这样。

public class AssignmentIII
{       
public static int numProcesses; // Represents the number of processes
public static int numResources; // Represents the number of different types of resources


public static int[] available ;
public static int[][] allocation ;
public static int[][] request ;
public static int[] work ;
public static Boolean[] finish ;

public static void main(String[] args) throws FileNotFoundException
{
    try
    {
        Scanner scan = new Scanner(System.in);
        Scanner fileScan = new Scanner(new File("input1.txt")); // Create file scanner

        System.out.println("Please enter the total number of processes: ");
        numProcesses = scan.nextInt();
        System.out.println("Please enter the number of different types of resources: ");
        numResources = scan.nextInt();

      available = new int[numResources]; // Create an emptry matrix for available processes
      allocation = new int[numProcesses][numResources]; // Create an empty allocation matrix nxm
      request = new int[numProcesses][numResources]; // Create an empty request matrix nxm
      work = new int[numResources]; // Create an empty work matrix
      finish = new Boolean[numProcesses]; // Create an empty finish matrix

        // Initialize the available matrix
        for(int i = 0; i < numResources; i++)
        available[i]=fileScan.nextInt();

        // Initialize the allocation matrix
        for(int j = 0; j < numProcesses; j++)
            for(int k = 0; k < numResources; k++)
                allocation[j][k]=fileScan.nextInt();

        // Initialize the request matrix
        for(int m = 0; m < numProcesses; m++)
            for(int n = 0; n < numResources; n++)
                request[m][n]=fileScan.nextInt();

        // Print allocation matrix
        System.out.println();
        System.out.println("Allocated");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("\tR" + i);
        }
        System.out.println();
        for(int j = 0; j < numProcesses; j++)
        {
            System.out.print("P" + j);
            for(int k = 0; k < numResources; k++)
            {
                System.out.print("\t" + allocation[j][k]);
            }
            System.out.println();
        }

        // Print available matrix
        System.out.println();
        System.out.println("Available");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("R" + i + "\t");
        }
        System.out.println();
        for(int i = 0; i < numResources; i++)
            System.out.print(available[i] + "\t");
        System.out.println();


        // Print request matrix
        System.out.println();
        System.out.println("Requested");
        for(int i = 0; i < numResources; i++)
            {
                System.out.print("\tR" + i);
            }
        System.out.println();

        for(int m = 0; m < numProcesses; m++)
        {
            System.out.print("P" + m);
            for(int n = 0; n < numResources; n++)
            {
                System.out.print("\t" + request[m][n]);
            }
            System.out.println();
        }
        System.out.println();

        // Begin deadlock detection algorithm               
        for(int i = 0; i < numResources; i++) // Intialize Work := Available
            work[i]=available[i];

        for(int j = 0; j < numProcesses; j++) // Check for Allocation != 0 and initialize Finish accordingly
        {
            int sumAllocation = 0;
            for(int i = 0; i < numResources; i++)
            {
                sumAllocation += allocation[j][i];
            }

            if (sumAllocation != 0)
                finish[j] = false;
            else finish[j] = true;
        }

        stepTwo();

        }
        catch(FileNotFoundException ex)
        {
        System.out.println("An error has occured. The file cannot be found.");
        }
}

public static void stepTwo()
{
    // Step 2
    // Find an index i where Finish[i] = false & Request[i] <= Work         
    for(int i = 0; i < numProcesses; i++)
    {
        int sumRequests = 0;
        int sumWork = 0;

        // Sum the Request and Work vectors
        for(int k = 0; k < numResources; k++)
        {   
            sumRequests += request[i][k];
            sumWork += work[k];
        }

        if (finish[i] == false && sumRequests <= sumWork)
        {
            finish[i] = true;
            for(int m = 0; m < numResources; m++)
            {
                work[m] = work[m] + allocation[i][m];
            }

            stepTwo();
        }
        else if (finish[i] == false)
        // Step 4: Print which processes are in a deadlock state
        // Print using P0, P1, ... , Pn format
            System.out.println("P" + i + " is in a deadlock state.");                           
    }
}

}

但这不是推荐的方式。因为这不是使用静态方法和静态属性的正确方法。此外,您应该使用 encapsulation。 使用更好的设计和封装方法,您的代码可以像这样改进。

    public class AssignmentIII
{       
    int numProcesses; // Represents the number of processes
    int numResources; // Represents the number of different types of resources
    String filepath;

    int[] available = new int[numResources]; // Create an emptry matrix for available processes
    int[][] allocation = new int[numProcesses][numResources]; // Create an empty allocation matrix nxm
    int[][] request = new int[numProcesses][numResources]; // Create an empty request matrix nxm
    int[] work = new int[numResources]; // Create an empty work matrix
    Boolean[] finish = new Boolean[numProcesses]; // Create an empty finish matrix

    public AssignmentIII(int numResources,int numProcesses, String filepath){
        this.numProcesses = numProcesses;
        this.numResources = numResources;
        this.filepath = filepath;

        available = new int[numResources]; // Create an emptry matrix for available processes
        allocation = new int[numProcesses][numResources]; // Create an empty allocation matrix nxm
        request = new int[numProcesses][numResources]; // Create an empty request matrix nxm
        work = new int[numResources]; // Create an empty work matrix
        finish = new Boolean[numProcesses]; // Create an empty finish matrix
    }

    public void initilizeMatrix() throws FileNotFoundException{
        Scanner fileScan = new Scanner(new File(filepath)); // Create file scanner

        // Initialize the available matrix
        for(int i = 0; i < numResources; i++)
            available[i]=fileScan.nextInt();

        // Initialize the allocation matrix
        for(int j = 0; j < numProcesses; j++)
            for(int k = 0; k < numResources; k++)
                allocation[j][k]=fileScan.nextInt();

        // Initialize the request matrix
        for(int m = 0; m < numProcesses; m++)
            for(int n = 0; n < numResources; n++)
                request[m][n]=fileScan.nextInt();

        fileScan.close();

    }



    public void print(){
        // Print allocation matrix
        System.out.println();
        System.out.println("Allocated");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("\tR" + i);
        }
        System.out.println();
        for(int j = 0; j < numProcesses; j++)
        {
            System.out.print("P" + j);
            for(int k = 0; k < numResources; k++)
            {
                System.out.print("\t" + allocation[j][k]);
            }
            System.out.println();
        }

        // Print available matrix
        System.out.println();
        System.out.println("Available");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("R" + i + "\t");
        }
        System.out.println();
        for(int i = 0; i < numResources; i++)
            System.out.print(available[i] + "\t");
        System.out.println();


        // Print request matrix
        System.out.println();
        System.out.println("Requested");
        for(int i = 0; i < numResources; i++)
        {
            System.out.print("\tR" + i);
        }
        System.out.println();

        for(int m = 0; m < numProcesses; m++)
        {
            System.out.print("P" + m);
            for(int n = 0; n < numResources; n++)
            {
                System.out.print("\t" + request[m][n]);
            }
            System.out.println();
        }
        System.out.println();

    }

    // Begin deadlock detection algorithm               
    public void deadLockdetecter(){
        for(int i = 0; i < numResources; i++) // Intialize Work := Available
            work[i]=available[i];

        for(int j = 0; j < numProcesses; j++) // Check for Allocation != 0 and initialize Finish accordingly
        {
            int sumAllocation = 0;
            for(int i = 0; i < numResources; i++)
            {
                sumAllocation += allocation[j][i];
            }

            if (sumAllocation != 0)
                finish[j] = false;
            else finish[j] = true;
        }
    }

    public  void stepTwo()
    {
        // Step 2
        // Find an index i where Finish[i] = false & Request[i] <= Work         
        for(int i = 0; i < numProcesses; i++)
        {
            int sumRequests = 0;
            int sumWork = 0;

            // Sum the Request and Work vectors
            for(int k = 0; k < numResources; k++)
            {   
                sumRequests += request[i][k];
                sumWork += work[k];
            }

            if (finish[i] == false && sumRequests <= sumWork)
            {
                finish[i] = true;
                for(int m = 0; m < numResources; m++)
                {
                    work[m] = work[m] + allocation[i][m];
                }

                stepTwo();
            }
            else if (finish[i] == false)
                // Step 4: Print which processes are in a deadlock state
                // Print using P0, P1, ... , Pn format
                System.out.println("P" + i + " is in a deadlock state.");                           
        }
    }

    public static void main(String[] args) throws FileNotFoundException
    {

        AssignmentIII assignment; 

        String p_filepath;
        int p_numProcesses;
        int p_numResources;

        p_filepath = "inpu1t.txt";

        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the total number of processes: ");
        p_numProcesses = scan.nextInt();
        System.out.println("Please enter the number of different types of resources: ");
        p_numResources = scan.nextInt();
        scan.close();

        assignment = new AssignmentIII(p_numResources, p_numProcesses, p_filepath);

        try
        {
            assignment.initilizeMatrix();
        }
        catch(FileNotFoundException ex)
        {
            System.out.println("An error has occured. The file cannot be found.");
        }

        assignment.stepTwo();

    }

}