0
public class Lab6 {

    public static void main(String[] args) {
        int List1[] = new int[10];
        List1[0] = 1;
        List1[1] = 3;
        List1[2] = 4;
        List1[3] = 5;
        List1[4] = 2;
        List1[5] = 6;
        List1[6] = 8;
        List1[7] = 9;
        List1[8] = 2;
        List1[9] = 7;
        toDisplay(List1);

    }
    public static void toDisplay (List1){
        int i;
        for(i=0; i>10; i++){
            System.out.print(List[i] + " ");
        }
    }
}

It will not carry over and recognize my List1 array. How do I display the List1 array in another method without making it a global?

4
  • 1
    This code doesn't compile Commented Mar 25, 2016 at 22:18
  • He has a back-slash after main(String[] args) {. Commented Mar 25, 2016 at 22:19
  • and the method declaration for toDisplay is invalid Commented Mar 25, 2016 at 22:20
  • 1
    You can write int[] list = {1, 3, 4, 5, 2, 6, 8, 9, 2, 7 }; Commented Mar 25, 2016 at 22:22

2 Answers 2

4

To pass an int[] to toDisplay (and the loop condition should be < not >). Something like

public static void toDisplay (int[] List1){
    int i;
    for (i=0; i < List1.length; i++) {
        System.out.print(List1[i] + " ");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Change List1 to int[] List1 in the formal parameters of the toDisplay method. By JCC, also you should rename the next names:

List1 -> list1
toDisplay -> display

What about this?

public static void display(int[] list){
    Arrays.stream(list).forEach(System.out::println);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.