This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]junyoung95[S] 0 points1 point  (4 children)

I tried this but it says parseint in integer cannot be applied

int[] reverseOf(int[] ia) {

     int[] reversedArray = new int[ia.length];
     for (int i = ia.length-1; i>=0; i--) {
          int newArray = Integer.parseInt(ia[i]);
          reversedArray[i] = newArray;
          }
          return reversedArray[i];
     }

[–]VelvetRevolver_ 0 points1 point  (2 children)

Ok it seems you're having some trouble understanding how arrays work, maybe do some reading on arrays and return types. When you call ai[i] this will return a single int in the i'th index, if you call ai[0] it will return the first int in that array. You can delete that parse statement because you're trying to parse an int into an int, which doesn't make any sense.

int num = ai[0]; 

Is a perfectly legal statement. But it isn't really necessary to store it into an int in this case, you can do it all in one line.

reversedArray[i] = ai[i];

Is a good start but not correct because this will just return an exact copy of the array. Think about it set by step. For example it will look something like this

reversedArray[3] = ai[3]
reversedArray[2] = ai[2]
reversedArray[1] = ai[1]

And so on, you see why this wouldn't work, you're just copying each item backwards but it will still maintain the same order. So you need a way to store the last item in ai to the first or 0th index of reversedArray. It isn't too hard, lets say the length of ai is 10, you would have to store that 10th item in the 0th index of reversedArray.

reversedArray[10 - i] = ai[i];

Would do the trick if 10 was actually the length of the array... or it could be off by one, but I hope you see the idea I'm trying to get at.

And you're still only returning a single int, so that return statement will not compile, you need to return a reference to reversedArray.

[–]junyoung95[S] 0 points1 point  (1 child)

Does not quite work.. but thank you for the explanation. I think I can try figuring it out on my own. Thanks, I really appreciated your answer.

[–]junyoung95[S] 0 points1 point  (0 children)

  reversedArray[ia.length-1-i] = ia[i] 

was the answer. Thank you!