将数组从一个方法返回到另一个方法

时间:2016-12-01 18:03:53

标签: java arrays

这是我的程序,每次它都要求我输入数组。 我想输入一次数组并处理该数组。 但是这个程序要求我再次输入数组 从名为“first”的方法我只想返回数组并使用该数组在两个不同的方法中添加和删除。但是,当我从main方法调用任何add或delete方法时,总是要求我输入每次第一个方法运行时所有数组元素。

包计划;

import java.util.Arrays;
import java.util.Scanner;

public class Functionality {
     public static int[] first( )
    {
        System.out.println("Enter the number of element in array");
        Scanner num = new Scanner(System.in);
        int data = num.nextInt();
        //return data;
        Scanner ar = new Scanner(System.in);
        int arr[] =  new int[data];
        System.out.println("Enter "+data+" Numbers");
        for(int i =0; i<data; i++){
            System.out.println("Enter NUmber :"+(i+1));
            arr[i] = num.nextInt();
    }
        System.out.println();
        System.out.println(Arrays.toString(arr));
    return arr; 
    }

    public static void main(String[] args) {
        add();
        delete();

        }
 static void add(){
int arr[]=first();
System.out.println("Enter the number you want to add");
Scanner one = new Scanner(System.in);
int naya = one.nextInt();
for(int i = 0; i<=arr.length-1; i++){
    arr[i]= arr[i] + naya;
}
System.out.println("The added array is");
System.out.println(Arrays.toString(arr));
}
 static void delete(){
    int arr[]=first();
    System.out.println("Enter the number you want to substract");
    Scanner two = new Scanner(System.in);
    int arko = two.nextInt();
    for(int i =0; i <= arr.length-1; i++ ){
        arr[i]=arr[i]-arko;
    }
    System.out.println("The Substracted array is");
    System.out.println(Arrays.toString(arr));

}

1 个答案:

答案 0 :(得分:1)

您应该获取对数组的引用并在后续方法调用期间传递它。

这应该可以解决问题:

class Functionality {

static int[] first() {
    System.out.println("Enter the number of element in array");
    Scanner num = new Scanner(System.in);
    int data = num.nextInt();
    //return data;
    Scanner ar = new Scanner(System.in);
    int arr[] = new int[data];
    System.out.println("Enter " + data + " Numbers");
    for (int i = 0; i < data; i++) {
        System.out.println("Enter NUmber :" + (i + 1));
        arr[i] = num.nextInt();
    }
    System.out.println();
    System.out.println(Arrays.toString(arr));
    return arr;
}

public static void main(String[] args) {
    int arr[] = first();
    add(arr);
    delete(arr);

}

static void add(int arr[]) {
    System.out.println("Enter the number you want to add");
    Scanner one = new Scanner(System.in);
    int naya = one.nextInt();
    for (int i = 0; i <= arr.length - 1; i++) {
        arr[i] = arr[i] + naya;
    }
    System.out.println("The added array is");
    System.out.println(Arrays.toString(arr));
}

static void delete(int arr[]) {
    System.out.println("Enter the number you want to substract");
    Scanner two = new Scanner(System.in);
    int arko = two.nextInt();
    for (int i = 0; i <= arr.length - 1; i++) {
        arr[i] = arr[i] - arko;
    }
    System.out.println("The Substracted array is");
    System.out.println(Arrays.toString(arr));

}

}

您的程序每次都要求您输入数组,因为每次调用方法first()都会创建新数组