0
String [] reverse = {"a", "b", "c", "1", "2", "3"};
for ( String forward : reverse )
    System.out.print ( forward + " ");
for ( int j = 0; j < array.length ; j++){
    //j = last index of reverse -1 everytime this loops
    reverse[i] = reverse[j]
}

I need to find a way to get values for primitive j? I've considered doing a nested for loop but that will result in too many loops. I could just do it manually by I want to do it efficiently as possible especially if I start to use longer strings.

3
  • A for loop upto (array.length)/2, a temp String variable and 3 assignment operations will do it for you :) Commented Nov 7, 2014 at 5:36
  • Could you elaborate on that? Commented Nov 7, 2014 at 5:38
  • Check AlexR's answer. Commented Nov 7, 2014 at 5:40

6 Answers 6

4

try this

    Collections.reverse(Arrays.asList(reverse));
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I'm refraining from learning things I haven't learnt myself yet, though I've added it to my "ThingsToLearn" List.
2

If you want to reverse the array you have stop your iterations in the middle of the array. Otherwise you reverse it twice and therefore it remains the same. Change your for loop to:

for ( int j = 0; j < array.length/2 ; j++){

Unfortunately I do not understand why do you need 2 loops. Just to print the initial array?

1 Comment

First one is to print the array, then I have to reprint it in reverse.
0

You can do something like this:-

String [] reverse = {"a", "b", "c", "1", "2", "3"}; 
StringBuilder builder = new StringBuilder(); //use String builder to build
for(String s : reverse) {
  builder.append(s);
 }
reverse = builder.reverse().toString().split("");

->Use StringBuilder to build a String

->Reverse the String

->Split it again to form an array of individual String elements

1 Comment

Ahh yeah, sorry again. If you look at my comment above, I want to learn how to do this with what I already know.
0

First, you could use Arrays.toString(Object[]) to print your String[]. Then you can iterate half of the array and swap the String at the current position with it's corresponding pair (at the length - current index, remembering that Java arrays are zero indexed so the last position in an array is array.length - 1). Finally, print again. Like,

String[] reverse = { "a", "b", "c", "1", "2", "3" };
System.out.println(Arrays.toString(reverse));
for (int i = 0, len = reverse.length / 2; i < len; i++) {
    String t = reverse[i];
    int p = (reverse.length - 1) - i; // 0,len - 1,len-1 - 2,len-2, etc.
    reverse[i] = reverse[p];
    reverse[p] = t;
}
System.out.println(Arrays.toString(reverse));

Output is

[a, b, c, 1, 2, 3]
[3, 2, 1, c, b, a]

2 Comments

why do you have 4 parameters for the for loop?
The len is declared as an int at the same time as i.
0

To reverse the array you only have to iterate half of the array and start swapping the element from start with end. swap element(position from start) with element(end position - position from start)

for (i = 0; i < reverse.length / 2; i++) {
  String temp = reverse[i];
  reverse[i] = reverse[reverse.length - 1 - i];
  reverse[reverse.length - 1 - i] = temp;
}

1 Comment

I appreciate the direct answer greatly, but I would prefer to understand this conceptually, could you explain your work for me?
0

By using Collections.reverse() method you can get the reverse elements list but first u have to add the string array to list.. String[] elements = {"a","b","c"}; List<String> list = Arrays. as-list(elements); Collections.reverse(list); after that you will iterate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.