Id like to know how to return a new array i wrote in a method below the main method.
just make a return type of array.
Example:
public int[] myIntArray() {
int[] myArr = new int[3]; // creating an array with length of 3
// assigning desired value for each element
myArr[0] = 1;
myArr[1] = 2;
myArr[2] = 3;
return myArr;
}
example codes above will return an array of int.
note: you can also create a method above your main method. the order/place of methods in your class doesn't matter.
I want to print the array but system.out.print doesn't work for arrays apprently.
you can iterate the array from first element to last element (or vice versa) and print it.
Example:
public static void main(String[] args) {
int[] myArr = {1, 2, 3}; //creating an array
// printing it using enhance loop
System.out.println("Printing array using enhance loop");
for (int i : myArr) {
System.out.println(i);
}
// printing array using index of element
System.out.println("Printing array using index");
for (int i = 0; i < myArr.length; i++) {
System.out.println(myArr[i]);
}
}
And lastly, I suggest you to read this
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)