我正在尝试打印出数组的所有元素,并将它们自己添加到我已经想到的索引中。我不知道如何自动化它。它有效,但它可以更好。
public class homeworkArrays
{
public static void main(String[] args)
{
int[] myArray = {6, 7, 2, 1, 10, 34};
int blank = addByIndex(myArray);
}
public static int addByIndex(int[] myArray)
{
for(int i = 0; i < myArray.length; i++)
{
System.out.println(i + " - " + myArray[i]);
}
myArray[0] = myArray[0] + myArray[0];
System.out.println("0 is now " + myArray[0]);
myArray[1] = myArray[1] + myArray[1];
System.out.println("1 is now " + myArray[1]);
myArray[2] = myArray[2] + myArray[2];
System.out.println("2 is now " + myArray[2]);
myArray[3] = myArray[3] + myArray[3];
System.out.println("3 is now " + myArray[3]);
myArray[4] = myArray[4] + myArray[4];
System.out.println("4 is now " + myArray[4]);
myArray[5] = myArray[5] + myArray[5];
System.out.println("5 is now " + myArray[5]);
int i = 0;
return myArray[i];
}
}