java - 将数组传递给另一个方法并复制它

标签 java arrays methods

我试图将数组从一种方法传递到另一种方法,然后将该数组的内容复制到新数组中。我在完成该任务的语法方面遇到问题。 有谁有一些我可以阅读的有关该主题的引用 Material ,或者可能是我可以应用的有用提示?

如果这是一个菜鸟问题,我深表歉意,但我只兼职搞了 3-4 周的 Java。

我知道 Java 使用按值传递,但我迷失的是......我应该在将 sourceArray 复制到 targetArray 之前调用它吗?

我的目标不仅仅是得到答案,我需要理解为什么。

谢谢...提前。

package cit130mhmw08_laginess;

import java.util.Scanner;

public class CIT130MHMW08_Laginess 
{

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the total number of dealers: ");
    int numDealers = input.nextInt();
    numDealers = numberOfDealers(numDealers);

    System.out.printf("%nPlease enter the required data for each of your dealers:");
    dataCalculation(numDealers);

}//main

//METHOD 1
public static int numberOfDealers(int dealers)
{
    int results;

    Scanner input = new Scanner(System.in);

    while(dealers < 0 || dealers > 30)
    {
        System.out.printf("%nEnter a valid number of dealers: ");
        dealers = input.nextInt();  
    }
    results = dealers;
    return results;

}//number of dealers methods

//METHOD 2
public static void dataCalculation(int data)
{
    String[] dealerNames = new String[data];

    Scanner input = new Scanner(System.in);

    System.out.printf("%nEnter the names of the dealers:%n ");

        for(int i = 0; i < data; i++)
        {
            String names =input.nextLine();
            dealerNames[i]= names;
        }

    int[] dealerSales = new int[data];
    System.out.printf("%nEnter their sales totals: %n");

        for(int i = 0; i < data; i++)
        {
            int sales = input.nextInt();
            dealerSales[i] = sales;
        }


    for(int i = 0; i < data; i++)
    {
        System.out.println(" " + dealerNames[i]);
        System.out.println(" " + dealerSales[i]);
    }
    //gather the required input data. 
    //Perform the appropriate data validation here.

}//data calculations

//METHOD 3
public static int commission(int data)
{
    //Create array
    int[] commissionRate = new int[dealerSales]; 

    //Copy dealerSales array into commissionRate
    System.arraycopy(dealerSales, 0, commissionRate, 0, dealerSales.length);

    //calculate the commission array.
    //$1 - $5,000...8%
    //$5,001 to $15,000...15%
    //$15,001...20%

    //

}//commission method 
}//class

最佳答案

如果要复制数组,可以使用 Arrays.copyOf(origin, length)方法。它需要两个参数,第一个是要从中复制数据的数组,第二个是新数组的长度,并导入 java.util.Arrays。

-请参阅链接了解更多信息 https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOf(int[],%20int)

关于java - 将数组传递给另一个方法并复制它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40181557/

相关文章:

java - 无法为安卓游戏绘制正方形网格

PHP:SQL 查询数组的最佳方法是什么? (如果你可以的话)

java - 如何在 Java 中调用 void 方法?

scala - Foo在Scala中不带参数编译错误?

java - 如何在jsp页面的<a>标签中使用onclick事件调用java函数

java - 比较器 : How to sort on two parameter when one is instance variable and other is reference in the Same Class

java - JMockit hibernate 模拟

javascript - JS : Array has no method 'keys' (Cordova, 安卓)

php - 如何计算特定数组值并在表数据中分配值

java - 对象中的方法 clone() 不可见?