I want to have multiple ASCII art figures which I want to store inside different arrays.
I learned about methods in Java that they are used for splitting code into smaller parts. Therefore, my idea is to put the art into individual arrays inside a method, so it's tidy.
Is there a way to do so? I made an example code to show how I imagine this.
My Google search about how to access a certain array inside a method didn't give me anything I can use. I apologize if there already is a thread about this topic.
public class Main {
public static void main(String[] args) {
System.out.println(divArr(arr2[1])); // expected output 20
}
public static void divArr() {
int arr1[] {1, 2, 3};
int arr2[] {10, 20, 30};
}
}
CodePudding user response:
This is not the idea of functions. functions are actions, they accept parameters, calculate stuff and return a value. for example this is a function that returns the average of 3 numbers:
public static int avarage(int a, int b, int c) {
int sum = a b c;
int result = sum / 3;
return result;
}
You probably found information about javascript's dictionary, which have different syntax from java's function, although both of them have {
and }
You can use the following approach:
public static class Main {
public static int[] arr1 = {1,2,3};
public static int[] arr2 = {10,20,30};
public static void main(String[] args) {
System.out.println(arr2[1]);
}
}
in this code, arr1
and arr2
are static variables.
You can also use a Map or a Class instead
EDIT:
another approach will be to use 2 dimensional array:
public static class Main {
public static int[][] frame1 = {
{1,2,3},
{10,20,30}
};
public static int[][] frame2 = {
{4,5,6},
{40,50,60}
};
public static void main(String[] args) {
System.out.println(frame1[1][1]);
}
}