Reputation: 85
My Code:
public static int invertieren(int[] werte) {
int[] werte1 = new int[werte.length];
for (int i = 0; i < werte.length; i++) {
for (int j = werte.length - 1; i < j; j--) {
werte1[j] = werte[i];
}
}
return werte1[0];
}
This is my code, I developed a Method that should reverse my Array. for example: in the main Method:
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,2,3,4,7,5};
System.out.println(invertieren(a));
}
and I need the that in position 0 - werte1[0]
should give me 5
back.
werte1[0] = 5
werte1[1] = 7
werte1[2] = 4
my new Array should look like that: int[] werte = {5,7,4,3,2,1}
my quastion I have not been answered before, cause I'm asking about my code, to learn from making it right.
Upvotes: 0
Views: 90
Reputation: 132
public static int[] invertieren(int[] werte){
int[] werte1 = new int[werte.length];
int length = werte.length-1;
for(int i= 0; i < werte.length; i++){
werte1[length-i] = werte[i];
}
return werte1;
}
Upvotes: 1
Reputation: 9049
You don't need nested loops to reverse the array. You just need to loop through the arrays like this:
static int[] invertieren(int[] werte) {
int[] werte1 = new int[werte.length];
for (int i = 0; i < werte.length; i++) {
werte1[i] = werte[werte.length - i - 1];
}
return werte1;
}
If you want to return the reversed array, you need to change the return type of your original method to return a int[]
instead of a int
, and then return werte1
instead of werte1[0]
.
To understand how this works assume that werte.length == 6
. So for i = 0
we have:
werte1[i] = werte[werte.length - i - 1]
werte1[0] = werte[6 - 0 - 1] = werte[5] = 5
For i = 1
:
werte1[1] = werte[6 - 1 - 1] = werte[4] = 7
And so on. So we go through werte1
from 0
to 5
storing the values from werte
from 5
to 0
.
If you analise how invertieren()
currently works you will see why the result is wrong:
Initial values:
werte = {1,2,3,4,7,5}
werte1 = {0,0,0,0,0,0}
for i = 0:
for j from 5 to 1:
werte1[j] = werte[0]
intermediate result: werte1 = {0,1,1,1,1,1}
for i = 1:
for j fom 5 to 2:
werte1[j] = werte[1]
intermediate result: werte1 = {0,1,2,2,2,2}
for i = 2:
for j from 5 to 3:
werte1[j] = werte[2]
intermediate result: werte1 = {0,1,2,3,3,3}
for i = 3:
for j from 5 to 4:
werte1[j] = werte[3]
intermediate result: werte1 = {0,1,2,3,4,4}
for i = 4:
for j from 5 to 5:
werte1[j] = werte[4]
final result: werte1 = {0,1,2,3,4,7}
for i = 5 it will do nothing, since j starts at 5.
It does not reverse the original array at all, so werte1[0] == 0
.
Upvotes: 2
Reputation: 1150
This is working code with a modified main
method:
public static int[] invertieren(int[] werte) {
int[] werte1 = new int[werte.length];
for (int i = 1; i <= werte.length; i++) {
werte1[i - 1] = werte[werte.length - i];
}
return werte1;
}
Your modified main
method:
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 7, 5};
for (int revVal : invertieren(a)) {
System.out.println(revVal);
}
}
Upvotes: 0
Reputation: 16711
You're incorrectly using a nested loop, use a single loop instead:
public int[] invertArray(int[] oldInts)
{
int[] newInts = new int[oldInts.length];
for (int i = oldInts.length - 1; i >= 0; i--)
{
newInts[oldInts.length - i - 1] = oldInts[i];
}
return newInts;
}
Upvotes: 1
Reputation: 166
If you are doing this for a class or for your own knowledge then this is not your answer. Otherwise, I would use Apache Commons ArrayUtils. They have several overloaded reverse methods.
Upvotes: 0
Reputation: 549
It looks as though you are returning the value at index 0.
public static int[] invertieren(int[] werte) {
int[] werte1 = new int[werte.length];
int len = werte.length - 1;
for (int i = 0; i < len; i++) {
werte1[len-i] = werte[i];
}
return werte1;
}
Upvotes: -1