About arrays and printing out the newly sorted array?

This is my code for reversing the order of an array:

int array[x];
String temp;

String reversedOrder = "";

for (int i = 0; i < x/2; i++)
 {
 temp = array[i];
 array[i] = array[x-1 - i];
 array[x-1 - i] = temp;
 reversedOrder = reversedOrder + array[i];
 }
System.out.println(reversedOrder);

My question is how to print out this newly sorted array? I try to declare a string and put it in the for loop but it doesnt print the way I wanted it to: lets say "Hello World I am here" I need it to print out "olleHdlroWImaereh" but all it prints are first two reversed words "olleHdlroW" and ends right there.

Any help is appreciated. Thank you.

*I am trying to write this program out on my OWN free time.

*Thanks to everyone for helping me with my program. It finally worked after adding a for loop similar to Elliot's. :p cheers.

Upvotes: 0

Views: 67

Answers (3)

Elliott Frisch
Elliott Frisch

Reputation: 201447

First, the easiest way is to use Arrays#toString(). Secondly, there are a few problems with your code,

int[] array = { 1, 2, 3 }; // <-- your array declaration isn't valid.
// String temp; // <-- A String is not an int.
int x = array.length; // <-- added to demonstrate

for (int i = 0; i < x / 2; i++) {
    // This is a swap with no temporary storage, also known as the
    // http://en.wikipedia.org/wiki/XOR_swap_algorithm
    array[i] ^= array[x - 1 - i];
    array[x - 1 - i] ^= array[i];
    array[i] ^= array[x - 1 - i];
}
System.out.println(Arrays.toString(array));

Output is

[3, 2, 1]

EDIT

Remove the Arrays.toString(), and replace it with something like this -

StringBuilder sb = new StringBuilder();
for (int i : array) {
    sb.append(i);
}
System.out.println(sb.toString());

Output is

321

EDIT 2

Or, you could use

for (int i : array) {
    System.out.print(i);
}
System.out.println();

Which will also output

321

Upvotes: 1

Braj
Braj

Reputation: 46841

Use Arrays class to print user friendly string of array

Arrays.toString(array);

Try this one:

    String[] array = new String[] { "Hello", "World", "I", "am", "here" };

    String[] newArray = new String[array.length];

    for (int i = 0; i < array.length; i++) {
        newArray[array.length - 1 - i] = new StringBuilder(array[i]).reverse().toString();
    }

    System.out.println(Arrays.toString(newArray));

--EDIT--

Use this one to print out array in string format without any commas and brackets

    String str = Arrays.toString(array).replaceAll(", ", "");
    System.out.println(str.substring(1, str.length() - 1));

Upvotes: 0

dting
dting

Reputation: 39287

Your reverse logic looks correct, however you are trying to append your reversedOrder string as you traverse only half of the charArray.

You can get your entire reversed charArray as a String by using the String(char[]) constructor after the for loop.

@Test
public void reverse() {
  char[] charArray = "Hello World I am here".toCharArray();
  int x = charArray.length;

  for (int i = 0; i < x/2; i++) {
    char temp = charArray[i];
    charArray[i] = charArray[x-1 - i];
    charArray[x-1 - i] = temp;
  }

  String expected = "ereh ma I dlroW olleH";
  assertEquals(expected, new String(charArray));
}

Upvotes: 0

Related Questions