简体   繁体   中英

How do I use parts of an old array to create a new one in java?

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.

您必须从java.util.Arrays导入Arrays

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;
  }
 }

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

How can I concatenate two arrays in Java?

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;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM