Wie erstellen Sie öffentliche Bereich zugänglich von allen Methoden, aber haben Benutzereingaben bestimmen Sie die Größe der es?
Ich habe mehrere arrays, deren Größe bestimmt werden müssen durch die Eingabe des Benutzers. Diese arrays zugegriffen werden soll, in der main-Methode sowie die stepTwo () - Methode. Jedoch komme ich nicht weiter. Die Benutzereingaben werden nicht kommen, bis die main-Methode, aber wenn ich das deklarieren der arrays in der main-Methode, dann habe ich keinen Zugriff auf die arrays in der stepTwo () - Methode. Ich würde es vorziehen, nicht zu passieren, die arrays als Parameter an stepTwo() als ich versuchte, Sie, dass, bevor aber es kam zu mehreren Fehlern. Irgendwelche Vorschläge? Siehe unten für komplette code:
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.");
}
}
}
- Ich dachte darüber nach, sich einfach nur die arrays 6x6 oder mit 6 Elementen, aber ich habe pseudocode für ein deadlock-Erkennung Algorithmus, der ich zu Folgen haben. Es werden verschiedene Sätze von Prozessen und Ressourcen, so dass die Anzahl der Elemente ändern sich und MÜSSEN genau in den arrays.
- Warum nicht teilen Sie die Deklaration und Initialisierung ?
- Beachten Sie, dass Sie nicht arrays zu erklären; deklarieren Sie die Variablen (oder Felder), die Verweise auf arrays, arrays erstellen und ordnen Sie Ihre Referenzen auf die Variablen.
- Ich wusste, es würde sein so etwas einfaches wie dieses. Ja, das vollständig das problem löst.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Deklarieren des Arrays, wie Sie tun - "über Haupt", aber initialisieren Sie nach der Lektüre in der entsprechenden Größe.
Erklärung:
Initialisierung: