English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Write the definition of a method reverse , whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value.

My code:
public void reverse(int [] arr)
{
String store = "";
for (int i = arr.length-1; i>=0; i--)
{
store += arr[i];
}
System.out.print(store);
}

2006-10-18 09:54:59 · 4 answers · asked by ? 1 in Computers & Internet Programming & Design

If you pass an array of integers arr1 containing the values 2, 4, 6, 8, 10 to reverse, and print out the array after that, the output will be

10, 8, 6, 4, 2

2006-10-18 10:15:04 · update #1

it says that I dont have the correct output

2006-10-18 13:41:12 · update #2

4 answers

You are correct. Here it is an a class:
public class ReverseArray{

public static void main(String[] args) {
int[] anArray = {2,4,6,8,10};
System.out.println(reverseArray(anArray));
}

public static String reverseArray(int[] arr){
String store = "";
for (int i = arr.length-1; i>=0; i--){
store += arr[i];
}
return store;
}
}

2006-10-18 11:13:35 · answer #1 · answered by Mark M 2 · 0 0

1

2017-01-20 03:45:34 · answer #2 · answered by ? 2 · 0 0

I think this is what you're after: public class ArrayStuff { /*** * Program: ArrayStuff.java * Purpose: Yahoo! Answers question. * Creator: Chris Clarke * Created: 15.04.2012 ***/ public static void main(String[] args) { int[] theArray = createArray(); printArray(theArray); } static int[] createArray() { return new int[8]; } static void printArray(int[] array) { for(int i=0; i< array.length; i++) System.out.println(array[i]); } }

2016-05-22 00:19:31 · answer #3 · answered by Anonymous · 0 0

It would help if you said what the problem/error is

2006-10-18 10:57:40 · answer #4 · answered by tru_story 4 · 0 0

fedest.com, questions and answers