我是新手Java程序员,我有一个问题! 我想访问数组的元素,而不是每次都重新激活以下方法:
public static int[] getValues(){
int[] vals = new int[2];
System.out.println("I only want this to print once");
//as an example just populating a simple array of size 2:
vals[0] = 1;
vals[1] = 2;
return vals;
}
现在在我的main方法中,当我想访问int [0]和int [1]时,如何在不重新激活整个方法的情况下再次启用print语句打印两次?
public static void main(String[] args){
int x = 0;
int y = 0;
x = getValues()[0];
y = getValues()[1];
System.out.println("x and y: "+x+" "+y);
}
这给出了一个输出:
I only want this to print once
I only want this to print once
x and y: 1 2