java - Can someone explain to me what is going on? there's a lot of things going on in which I haven't been taught yet
public class Main {
public static void main(String[] args) {
int[] x = {1, 2, 3};
increase(x);
Reverse rev = new Reverse();
rev.reverse(x);
System.out.println(x[0] + " " + x[1] + " " + x[2]);
}
//Increase every element in the array by 1
/开发者_如何学Go/For example: array : 0, 1, 2 will become 1, 2, 3
public static void increase(int[] a) {
//TODO: FILL ME
}
}
class Reverse {
//Reverse the array
//For example: array 0, 1, 2 will become 2, 1, 0
public void reverse(int[] a) {
//TODO: FILL ME
}
}
What does increase(x); do ? Basically I have to fill in where he wrote fill in. But its kind of hard when i dont even understand what is going on.
For now increase()
does nothing for now.
Your assignment is to write the content of the method in a way that will increase the content of every cell of your array.
What does your code do?
int[] x = {1, 2, 3}; // Create an array with 3 elements, "1", "2" and "3"
increase(x); // Call the increase() method which for now does nothing
Reverse rev = new Reverse(); // Create an instance of the Reverse class, which contains a method to reverse arrays (but does nothing for now)
rev.reverse(x); // Call the famous reverse() method.
System.out.println(x[0] + " " + x[1] + " " + x[2]); //Print the content of you array x[0] is the first cell, x[1] the second, etc.
The statement increase(x)
calls the static method increase(int[] a)
.
As the method just contains a comment, it effectively does nothing. Looks like your tutor wants you to replace the //TODO: FILL ME
comment with code that implements the increase method correctly. Your tutor has described what the code should do in the comments above the method;
//Increase every element in the array by 1
//For example: array : 0, 1, 2 will become 1, 2, 3
Good luck!
What you want to do is in the increase method, iterate through all the values in the integer array "a" using a for loop. You should then assign an integer that's 1 more than the current integer to itself.
I'm so tempted to post a solution but that'll teach you nothing!
精彩评论