I wrote code to reverse an integer array. Code is as shown below:
public class ReverseArray {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
for (int i = 0; i <= arr.length/2; i++)
int temp = arr[0];
arr[0] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
System.out.println(arr);
} catch (Exception e) {
System.out.println(e);
}
}
}
But it's not reversing the array.. I'm getting following output.
[I@3bad086a
I don't see anything wrong with my logic.
That is printing out the reference to the array.
If you print out the array one element at a time, you will see the reversed array.
EDIT: Two more points.
arr[0]
when you intend to use arr[i]
. arr.length / 2
. Since this is a homework question, I leave it to you to find out why; try printing out the intermediate results and explaining them. 好吃的:
System.out.println(Arrays.toString(arr));
u need to loop through the array to print it
for (int i = 0; i <arr.length; i++) {
System.out.println(arr[i]);
}
Firstly , i will let the above code remain (it is wrong ( ArrayIndexOutOfBoundsException
), and now i believe it was good that you down-voted me )
public class ReverseArray {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
for (int i = 0; i <= arr.length/2; i++){
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
for (int i = 0; i <= arr.length -1 ; i++){
System.out.println(arr[i]);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
For now , i will make a few simple suggestions
You should use Arrays.toString(arr)
for that. Arrays don't have specific implementation of toString()
, so calling it results in the default toString
implementation of Object. In Java5 the toString
functionality was added for arrays, as a static method in Arrays class. I guess that it wasn't added to arrays directly due to backward compatibility considerations.
And by the way, you have a small bug in the code. I believe you want to swap values in indexes length-i-1
and i
(not 0
).
you need to print the contents of the array not the array itself. try using a loop to iterate over the array and print each int one at a time.
use following to print your array
for (int i = 0; i <arr.length; i++) {
System.out.println(arr[i]);
}
instead of
System.out.println(arr);
Just do
for (int i = 0; i <arr.length; i++) {
System.out.println(arr[i]);
}
You are printing
System.out.println(arr);
This will going to print memory location of variable arr.
You should try
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
Regarding printing the array, several of the previous answers are correct. However, your code still doesn't work. You need to replace arr[0]
with arr[i]
in your loop.
Some of the easiest approaches to reverse an array in Java.
package reversearray;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
final public class Main
{
private Object reverseArray(Object[]arr)[]
{
List<Object>list=Arrays.asList(arr);
Collections.reverse(list);
return(list.toArray());
}
public static void main(String... args)
{
Object[] arr ={1, 2, 3, 4, 5};
arr=new Main().reverseArray(arr);
int newArray[]=new int[arr.length]; //Array of your desired type.
for(int i=0;i<arr.length;i++)
{
newArray[i]=Integer.valueOf(arr[i].toString());
}
for(int i=0;i<arr.length;i++)
{
System.out.println(newArray[i]);
}
}
}
You can simply use with Commons.Lang as follows.
ArrayUtils.reverse(int[] arr)
Using guava :
Collections.reverse(Ints.asList(arr));
from my limited recall of C
for i = (length( array ) -1 ) to 0
{ print( array( i ) ; i-- }
NOTE: you are counting ELEMENTS not BYTES ALSO: you subtract 1 from length because you are counting down to ZERO not 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.