java Java中不同类之间传递数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17243669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing array elements between different classes in Java
提问by bobby gutierrez
I am having trouble passing the elements in an array that holds names and numbers to another class where calculations will be done. This is what I have for the classes.
我无法将包含名称和数字的数组中的元素传递给将进行计算的另一个类。这就是我的课程。
public class Employee {
private int sales;
private String name;
public Employee(String name, int sales){
this.name = name;
this.sales = sales;
}
public String getName(){
return this.name;
}
public int getSales(){
return this.sales;
}
}
That class is to help store the name and sales for the array. Here is the array in its separate class.
该类用于帮助存储数组的名称和销售额。这是其单独类中的数组。
import java.util.Scanner;
public class EmployeeArray {
public static int employee() {
Scanner input = new Scanner(System.in);
int numEmp;
System.out.println("Enter how may employees to be compared: ");
numEmp = input.nextInt();
input.nextLine();
Employee[] employees = new Employee[numEmp];
for (int i = 0; i < numEmp; i++) {
System.out.print("Enter your name: ");
String employeeName = input.nextLine();
System.out.println();
System.out.print("Enter your annual sales: ");
int employeeSales = input.nextInt();
input.nextLine();
System.out.println();
Employee employee = new Employee(employeeName, employeeSales);
employees[i] = employee;
}
Employee maxSeller = employees[0];
for (int i = 0; i < employees.length; i++) {
System.out.println("Employee Name: " + employees[i].getName());
System.out.println("Total sales: $" + employees[i].getSales());
System.out.println();
if (maxSeller.getSales() < employees[i].getSales()) {
maxSeller = employees[i];
}
}
System.out.println();
System.out.println("Top Seller is: " + maxSeller.getName());
System.out.println("Top Sales are: $" + maxSeller.getSales());
return maxSeller.getSales();
}
}
The array holds the values and returns a top seller called maxSeller and gets the name and sales amount for them. Now the problem I am running into is that in a separate class I need to get the values stored in this array to do some calculations. I have to get sales of maxSeller and compare them to the sales of the other sellers in the array. Form there I am trying to display how much more sales the others need to get to equal or surpass the top seller. That is the problem I am running into. I don't know how to pass the values from this array to the next class so I can do the calculations. Any thoughts?
该数组保存这些值并返回一个名为 maxSeller 的畅销商品,并获取它们的名称和销售额。现在我遇到的问题是,在一个单独的类中,我需要获取存储在该数组中的值来进行一些计算。我必须获得 maxSeller 的销售额并将它们与数组中其他卖家的销售额进行比较。从那里我试图显示其他人需要获得多少销售额才能达到或超过最畅销的产品。这就是我遇到的问题。我不知道如何将值从这个数组传递到下一个类,以便我可以进行计算。有什么想法吗?
回答by arynaq
Instead of creating the array inside a static method you can make it a member of EmployeeArray
you can then reuse the the array in a static context (not recommended) or create different EmployeeArray
s to hold different arrays of employees, this makes more sense in OOP. A rough sketch:
而不是在静态方法中创建数组,您可以使其成为您的成员,EmployeeArray
然后可以在静态上下文中重用该数组(不推荐)或创建不同的EmployeeArray
s 来保存不同的员工数组,这在 OOP 中更有意义。一个粗略的草图:
import java.util.Scanner;
public class EmployeeArray {
private Employee[] employees;
public Employee[] getEmployees() {
return employees;
}
public void setUpArray() {
Scanner input = new Scanner(System.in);
int numEmp;
System.out.println("Enter how may employees to be compared: ");
numEmp = input.nextInt();
input.nextLine();
employees = new Employee[numEmp];
for (int i = 0; i < numEmp; i++) {
System.out.print("Enter your name: ");
String employeeName = input.nextLine();
System.out.println();
System.out.print("Enter your annual sales: ");
int employeeSales = input.nextInt();
input.nextLine();
System.out.println();
Employee employee = new Employee(employeeName, employeeSales);
employees[i] = employee;
}
}
public int getMaxSellerEmployeeSales() {
Employee maxSeller = employees[0];
for (int i = 0; i < employees.length; i++) {
System.out.println("Employee Name: " + employees[i].getName());
System.out.println("Total sales: $" + employees[i].getSales());
System.out.println();
if (maxSeller.getSales() < employees[i].getSales()) {
maxSeller = employees[i];
}
}
System.out.println();
System.out.println("Top Seller is: " + maxSeller.getName());
System.out.println("Top Sales are: $" + maxSeller.getSales());
return maxSeller.getSales();
}
public static void main(String[] args) {
EmployeeArray google = new EmployeeArray();
google.setUpArray();
EmployeeArray ms = new EmployeeArray();
ms.setUpArray();
EmployeeArrayCalculatorAndManipulator eGoogle = new EmployeeArrayCalculatorAndManipulator();
EmployeeArrayCalculatorAndManipulator eMS = new EmployeeArrayCalculatorAndManipulator();
eGoogle.doSomethingWithArray(google.getEmployees());
eMS.doSomethingWithArray(ms.getEmployees());
}
}
回答by Diksha Nain
It worked out for me:-
它对我有用:-
package EmployeePackage;
包裹员工包裹;
public class Employee {
公共类员工{
private int sales;
private String name;
public Employee(String employeeName, int employeeSales){
this.name = employeeName;
this.sales = employeeSales;
}
public String getName(){
return this.name;
}
public int getSales(){
return this.sales;
}
}
}
package EmployeePackage;
包裹员工包裹;
import java.util.Scanner; public class EmployeeArray {
导入 java.util.Scanner; 公共类员工数组 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numEmp;
System.out.println("Enter how may employees to be compared: ");
numEmp = input.nextInt();
input.nextLine();
Employee[] employees = new Employee[numEmp];
for (int i = 0; i < numEmp; i++) {
System.out.print("Enter your name: ");
String employeeName = input.nextLine();
System.out.println();
System.out.print("Enter your annual sales: ");
int employeeSales = input.nextInt();
input.nextLine();
System.out.println();
employees[i] = new Employee(employeeName , employeeSales); //calling constructor
}
Employee maxSeller = employees[0];
for (int i = 0; i < employees.length; i++) {
System.out.println("Employee Name: " + employees[i].getName());
System.out.println("Total sales: $" + employees[i].getSales());
System.out.println();
if (maxSeller.getSales() < employees[i].getSales()) {
maxSeller = employees[i];
}
}
System.out.println();
System.out.println("Top Seller is: " + maxSeller.getName());
System.out.println("Top Sales are: $" + maxSeller.getSales());
}
}
}