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


i have multiple arrays sizes need determined user input. these arrays should accessible in main method steptwo() method. however, stuck. user input doesn't come until main method, if declare arrays in main method, can't access arrays in steptwo() method. prefer not pass arrays parameters steptwo() tried before came multiple errors. suggestions? see below complete code:

    public class assignmentiii     {            public static int numprocesses; // represents number of processes     public static int numresources; // represents number of different types of resources      public static int[] available = new int[numresources]; // create emptry matrix available processes     public static int[][] allocation = new int[numprocesses][numresources]; // create empty allocation matrix nxm     public static int[][] request = new int[numprocesses][numresources]; // create empty request matrix nxm     public static int[] work = new int[numresources]; // create empty work matrix     public static boolean[] finish = new boolean[numprocesses]; // create 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 total number of processes: ");             numprocesses = scan.nextint();             system.out.println("please enter number of different types of resources: ");             numresources = scan.nextint();              // initialize available matrix             for(int = 0; < numresources; i++)             available[i]=filescan.nextint();              // initialize allocation matrix             for(int j = 0; j < numprocesses; j++)                 for(int k = 0; k < numresources; k++)                     allocation[j][k]=filescan.nextint();              // initialize 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 = 0; < 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 = 0; < numresources; i++)             {                 system.out.print("r" + + "\t");             }             system.out.println();             for(int = 0; < numresources; i++)                 system.out.print(available[i] + "\t");             system.out.println();               // print request matrix             system.out.println();             system.out.println("requested");             for(int = 0; < 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 = 0; < numresources; i++) // intialize work := available                 work[i]=available[i];              for(int j = 0; j < numprocesses; j++) // check allocation != 0 , initialize finish accordingly             {                 int sumallocation = 0;                 for(int = 0; < 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. file cannot found.");             }     }      public static void steptwo()     {         // step 2         // find index finish[i] = false & request[i] <= work                  for(int = 0; < numprocesses; i++)         {             int sumrequests = 0;             int sumwork = 0;              // sum request , 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 processes in deadlock state             // print using p0, p1, ... , pn format                 system.out.println("p" + + " in deadlock state.");                                    }     }  } 

declare array - "above main", initialize after reading in appropriate size.

declaration:

public static int[] available; 

initialization:

available = new int[numresources]; 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

how does one get csharp-sqlite to throw exceptions for duplicates or foreign key constraint violations -

Simple Angular 2 project fails 'Unexpected reserved word' -