1

So I need to create a new array that swaps the first and last values of my existing array. Here's what I have so far:

import java.util.Scanner;
import java.util.Arrays;
public class P9_ArrayManipulate
{    public static void main (String[] args)
 {   Scanner in = new Scanner(System.in);
    System.out.println("Please enter seven numbers for an Array.");
    int []array = new int[7];
    int total = 0;
    for (int i = 0; i < 7;i++){
            array[i] = in.nextInt();
    }
    System.out.println("Your reversed array is "+reverseArray(array));
    System.out.println("Your swapped array is "+swapArray(array));
    System.out.println("Your array without even numbers is "+evenArray(array));
    System.out.println("The program has finished calling array methods.");
}
 public static int []reverseArray(int []array)
{   int[] reversed = new int[array.length];
    int i;
    for(i=0; i < array.length; i++)
    {
        reversed[i] = array[(array.length - i -1)];
    }
    return reversed;
}
public static int []swapArray (int []array)
{   
  array[0] += array[array.length-1];
  array[array.length-1] = array[0] - array[array.length-1];
  array[0] -= array[array.length-1];
  return array;
}
public static int []evenArray(int []array)
{   for (int i = 0; i < array.length;i++){
    if (array[i]%2 !=0)
    {array[i] = 0;}
    i++;}
    return array; 
}

}

Everything compiles correctly (as far as I can tell), but I'm getting a weird run time error. After seven inputs, I get this as my output: Your reversed array is [I@b4d079 Your swapped array is [I@3644d1 Your array without even numbers is [I@3644d1 The program has finished calling array methods.

Colleen
  • 13
  • 4
  • What is the error? Is it the weird formatting of the messages? That's because arrays are not automatically converted to strings. You might want to try System.out.println("Your reversed array is "+Arrays.toString(reverseArray(array))); – Ken Geis Nov 04 '15 at 05:24
  • Ah, got it. Thanks so much. – Colleen Nov 04 '15 at 05:29

4 Answers4

0

You have to import Arrays from java.util.Arrays

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
  • Got it, now I'm having issues combining the parts of the old array into the new one. It doesn't like the +. – Colleen Nov 04 '15 at 04:22
0

Try something like this:

*Edit: If its a must to create new array, just create int newarray[] = method(); instead of array[]=method();

  import java.util.Scanner;
  public class Array 
 {
public static void main (String[] args)
{   Scanner in = new Scanner(System.in);

System.out.println("Please enter seven numbers for an Array.");
int []array = new int[7];
for (int i = 0; i < 7;i++){
    array[i] = in.nextInt();
}

array = reverseArray(array);
System.out.println("Your reversed array is :");
for (int i = 0; i < array.length; i++) {
    System.out.print(array[i]+" ");
}
System.out.println();

array = swapArray(array);
System.out.println("Your swapped array is :");
for (int i = 0; i < array.length; i++) {
    System.out.print(array[i]+" ");
}
//          System.out.println("Your array without even numbers is "+evenArray(array));
System.out.println();
System.out.println("The program has finished calling array methods.");
}
public static int[] reverseArray(int []array)
{   int left = 0;
int right = array.length-1;
while(left<right){
    int reverse = array[left];
    array[left]=array[right];
    array[right] = reverse;
    left++;
    right--;
}
return array;
}
public static int []swapArray (int []array)
{   
    int help = 0;
    help = array[0];
    array[0] = array[6];
    array[6] = help;

    return array;
  }
 }
Sekula1991
  • 288
  • 4
  • 17
0

You may use the following answer to merge the arrays in swapArray() method

How can I concatenate two arrays in Java?

Community
  • 1
  • 1
Hasan
  • 21
  • 3
0

It would be much easier to just swap the first and last values of the array passed in to the swapArray() function and return that instead of creating a new array.

Something like the following will work (and it doesn't create a new variable to facilitate swapping):

public static int[] swapArray(int[] array) {
    array[0] += array[array.length-1];
    array[array.length-1] = array[0] - array[array.length-1];
    array[0] -= array[array.length-1];
    return array;
}
Shadow
  • 3,926
  • 5
  • 20
  • 41