java - Accessing an array in another method -
i trying access array in separate method initialized in.
public void initializearray() { string sentences[] = new string[5]; for(int i=0; i<5; i++) { sentences[i] = i+1; } } public void printarray() { for(int i=0; i<5; i++) { system.out.println(sentences[i]); } }
i know in 1 loop, can explain how print array way? need access sentences array in separate method initialized in. tried make instance of array @ top of program gives me error saying "local variable hides field".
i tried make instance of array @ top of program gives me error saying "local variable hides field".
you have instance variable, remove local variable within method:
public void initializearray() { //string sentences[] = new string[5]; ... }
also don't use magic numbers did in for-loop:
for(int i=0; i<5; i++)//use `sentences.length` instead of `5`