Reputation: 41
I'm doing an exercise about the method and one-dimensional arrays. I created an array under a method. How can I use that array under another method? For example, I determined a few identities in my first method and the second, I will extract some features from those identities using these identities. Let's say that the more times "xyz" are written in this code, I will increase their health by 1.
Upvotes: 0
Views: 1009
Reputation: 156
well there are two things 1st - As alex mentioned 2nd you can create array in that function
public static String[] population(int n){
String[] IDs = new String[n];
String[] letter = {"x","y","z"};
for (int j=0; j<n; j++){
IDs[j] = "";
for (int i=0; i<128; i++){
Random rand = new Random();
int w = rand.nextInt(letter.length);
IDs[j]+=letter[w];
}
}
return IDs;
}
private static void calculateHealth(int health) {
String[] Ids = population(health);
health =0;
for (int i=2; i<128; i++){
if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
health+=1;
}
}
System.out.println(health);;
}
Upvotes: 0
Reputation: 5004
The problem is that the Array ID's is currently a local variable of your method population
Here some important Points about Variable in scope in Java:
Scope means that variables are only accessible inside the region they are created.
Here an example to show this behavior
public class Main {
public static void main(String[] args) {
// Code here CANNOT use x
{ // This is a block
// Code here CANNOT use x
int x = 100;
// Code here CAN use x
System.out.println(x);
} // The block ends here
// Code here CANNOT use x
}
}
In your case you can define the id's in a "higher scope" outside of your population method.
public class L {
public static String[] IDs = new String[n];
public static void main(String[] args) {
}
public static void population(int n){
String[] letter = {"x","y","z"};
for (int j=0; j<n; j++){
IDs[j] = "";
for (int i=0; i<128; i++){
Random rand = new Random();
int w = rand.nextInt(letter.length);
IDs[j]+=letter[w];
}
}
}
private static void calculateHealth(int health) {
population(health);
health =0;
for (int i=2; i<128; i++){
if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
health+=1;
}
}
System.out.println(health);;
}
}
Then you can use it in both of your methods.
Here is another solution for this problem where you don't have to care about scopes. You can just pass to each of the functions an array as parameter.
public class L {
public static void main(String[] args) {
}
public static void population(String[] IDs, int n){
String[] letter = {"x","y","z"};
for (int j=0; j<n; j++){
IDs[j] = "";
for (int i=0; i<128; i++){
Random rand = new Random();
int w = rand.nextInt(letter.length);
IDs[j]+=letter[w];
}
}
}
private static void calculateHealth(String[] IDs, int health) {
population(health);
health =0;
for (int i=2; i<128; i++){
if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
health+=1;
}
}
System.out.println(health);;
}
}
Upvotes: 2
Reputation: 76639
Just declare the String[]
outside of the method declaration; setting it's dimension is useless:
String[] IDs = new String[]{};
And you probably don't need these static
keywords either.
Upvotes: 0