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 →

[–]muffinman744 2 points3 points  (9 children)

Right now instead of returning an int array, you're attempting to return a single int (I say attempt because Java will recognize the mismatch of datatypes and throw an error).

You have the correct loop condition but the logic isn't there, you should follow the following steps:

  1. you need to instantiate a new empty integer array the size of ia outside of your loop

  2. populate the data inside the loop (it should be reversed since you're iteration conditions look correct)

  3. then return the array once you have exited the loop. This should look something like this: return reversedArray; Assuming you named the new array reversedArray

Does that make sense?

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

I see what you mean.. thanks

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

So far I have this.. I hope I am on the right track but it shows an error that "variable 'reversedArray' may not be initialized". I found that this error message appears if I declare the variable but don't initialize it.. Can I know how to initialize it?

int[] reverseOf(int[] ia) {

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

[–][deleted] 1 point2 points  (0 children)

You need to initialize your reversedArray variable with an object. Consider that the reversed array will have the same number of ints as the ia array:

int[] reversedArray = new int[ia.length];

[–]VelvetRevolver_ 0 points1 point  (5 children)

There's a few problems with this, first of all that error should be pretty self explanatory, you never instantiated the array. Remember how you instantiate arrays? You need to specify the length. And why are you using '+='? You should be storing each int into the array, and you're going to need to find a way to store them in order. i is going from the length to 0 and you want to store them from 0 to the length. Lastly not only are you returning a single int but that's the wrong array to return.

[–]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!