Hey guys! So I'm trying to create a method that takes in a list of integers into an array and prints the elements inside the array line by line. How do I print the the elements? Here's what I have so far:

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


    }

    public static int[] printElements(int num){
       int[] array = new int[] {1,2,3,4,5};

       for (int i = 0; i < array.length; i++){
           System.out.print(array[i]);
        }
        System.out.println();

        return array;
    }

}

You need to read about method declaration syntax before you can go any further. The reason you are not getting it is because you declare it wrong. You need to know which part means what...

public static int[] printElements(int num)
  ^      ^      ^         ^         ^
  |      |      |         |         |
modifier |    return   method     argument
     modifier  type     name        list

Edited 4 Years Ago by Taywin

I've read the link you posted. I've editted my code, my only problem is that how do I access the printElements method from main? Sorry, I'm still quite new to java.. still trying to get understand everything. :D

Edited 4 Years Ago by laguardian

how do I access the printElements method from main

Since that method is static you can call it directly from the main() method without creating an instance of the class.

This article has been dead for over six months. Start a new discussion instead.