如何在JAVA中动态创建数组?

发布于 2024-12-16 20:25:46 字数 1849 浏览 0 评论 0原文

我的问题很简单,我想创建一个数组并用一些字符串项填充它。下面是一个简单代码,包含两个类一个主要方法(非常基本的程序)。

public class CompanyDetails {
public static void main (String[] args){

    CompanyInput apple = new CoInput();
    apple.coDetailsSummary("Apple","USA", "NA");
    apple.computeNoOfJoinees();

    CompanyInput htc = new CoInput();
    htc.coDetailsSummary("HTC", "Thailand", "NA");
    htc.computeNoOfJoinees();

}

public class CompanyInput {
float noOfJoinees;
String companyName;
String address;
String phoneNumber;
int randomInt,randomInt1;
int noOfEmployees;
Array gh;

void coDetailsSummary(String companyName, String address, String phoneNumber) {
    // TODO Auto-generated method stub

    System.out.println("Company Name                              : "+companyName); 
    System.out.println("Address                                   : "+address); 
    System.out.println("Contact Information                       : "+phoneNumber); 

}
void computeNoOfJoinees(){
    float m1,m2, m3;
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx)
        randomInt = randomGenerator.nextInt(30);
        randomInt1 = randomGenerator.nextInt(20);

        m1 = randomInt;
        m2 = randomInt1;
    m3 = m1+m2/100;
    System.out.print("The number of joinees joining every month : "+m3);
    System.out.println("\n");
}

我想创建一个名称为“companyName”的数组,并用将作为输出给出的所有值填充

例如,

Company Name                              : Apple
Address                                   : USA
Contact Information                       : NA
The number of joinees joining every month : 13.0

当我运行程序时,所有这些值都必须自动插入到数组中。结果应该是这样的,

Apple[] = {Apple,USA,NA,13.0}

我怎样才能实现这个目标?

My question is quite simple I would like to create an array and populate it with some string items. Below is a simple code containing two classes and one main method (pretty basic program).

public class CompanyDetails {
public static void main (String[] args){

    CompanyInput apple = new CoInput();
    apple.coDetailsSummary("Apple","USA", "NA");
    apple.computeNoOfJoinees();

    CompanyInput htc = new CoInput();
    htc.coDetailsSummary("HTC", "Thailand", "NA");
    htc.computeNoOfJoinees();

}

}

public class CompanyInput {
float noOfJoinees;
String companyName;
String address;
String phoneNumber;
int randomInt,randomInt1;
int noOfEmployees;
Array gh;

void coDetailsSummary(String companyName, String address, String phoneNumber) {
    // TODO Auto-generated method stub

    System.out.println("Company Name                              : "+companyName); 
    System.out.println("Address                                   : "+address); 
    System.out.println("Contact Information                       : "+phoneNumber); 

}
void computeNoOfJoinees(){
    float m1,m2, m3;
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx)
        randomInt = randomGenerator.nextInt(30);
        randomInt1 = randomGenerator.nextInt(20);

        m1 = randomInt;
        m2 = randomInt1;
    m3 = m1+m2/100;
    System.out.print("The number of joinees joining every month : "+m3);
    System.out.println("\n");
}

}

I would like to create an array with name as the "companyName" and populate it with all the values that will be given as output.

For e.g

Company Name                              : Apple
Address                                   : USA
Contact Information                       : NA
The number of joinees joining every month : 13.0

All of these values must be inserted into an array automatically when I run the program. The result should look like this

Apple[] = {Apple,USA,NA,13.0}

how can I achieve this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

腹黑女流氓 2024-12-23 20:25:46

使用ArrayList,动态添加元素
在您的情况下,它将是 CompanyInput 对象的 ArrayList。

ArrayList<CompanyInput>companyName = new ArrayList<CompanyInput>();
companyName.add(new CompanyInput("Apple","USA","NA",13.0));
companyName.add(new CompanyInput("MSoft","USA","NA",15.0));

等等...

希望有帮助!

Use an ArrayList, for dynamically adding elements
In your case, it would be an ArrayList of CompanyInput objects.

ArrayList<CompanyInput>companyName = new ArrayList<CompanyInput>();
companyName.add(new CompanyInput("Apple","USA","NA",13.0));
companyName.add(new CompanyInput("MSoft","USA","NA",15.0));

etc...

Hope that helps !

小苏打饼 2024-12-23 20:25:46

将您更改

float noOfJoinees;
String companyName;
String address;
String phoneNumber;
int randomInt,randomInt1;
int noOfEmployees;
Array gh;

void coDetailsSummary(String companyName, String address, String phoneNumber) {
    // TODO Auto-generated method stub

    System.out.println("Company Name                              : "+companyName); 
    System.out.println("Address                                   : "+address); 
    System.out.println("Contact Information                       : "+phoneNumber); 

}

void coDetailsSummary(String argCompanyName, String argAddress, String argPhoneNumber) {
    // TODO Auto-generated method stub
this.companyName = argCompanyName

对于其他参数,类似地

}

// 。然后将其添加到 ArrayList 并循环遍历它们并打印它。

Change your

float noOfJoinees;
String companyName;
String address;
String phoneNumber;
int randomInt,randomInt1;
int noOfEmployees;
Array gh;

void coDetailsSummary(String companyName, String address, String phoneNumber) {
    // TODO Auto-generated method stub

    System.out.println("Company Name                              : "+companyName); 
    System.out.println("Address                                   : "+address); 
    System.out.println("Contact Information                       : "+phoneNumber); 

}

into

void coDetailsSummary(String argCompanyName, String argAddress, String argPhoneNumber) {
    // TODO Auto-generated method stub
this.companyName = argCompanyName

// similarly for the other arguments.

}

And then add it to a ArrayList and loop through them and print it.

眼眸 2024-12-23 20:25:46

在main方法中,创建一个CompanyInput类型的ArrayList,如下所示:
列表 listOfCompanies = new ArrayList();

现在,将 CompanyInput 的所有实例一一添加到此列表中。例如
要将“apple”添加到列表中:
listOfCompanies.add(苹果);

还将 Object 类的 toString 方法重写到 CompanyInput 类中。要了解如何覆盖 toString 您可以参考: http://www.javabeat.net/tips/12-overriding-the-tostring-method-in-object-cl.html

现在,只需打印列表,就像打印任何变量一样:-
System.out.println(公司列表);

对于添加到列表中的所有实例,重写的 toString 方法的输出将打印到控制台。

In the main method, create an ArrayList of type CompanyInput as follows:
List listOfCompanies = new ArrayList();

Now, one by one add all the instances of CompanyInput to this list. For e.g.
To add "apple" to the list:
listOfCompanies.add(apple);

Also Override the toString method of Object class into the CompanyInput class. To know how to overirde toString you can refer : http://www.javabeat.net/tips/12-overriding-the-tostring-method-in-object-cl.html

Now, just print the list just like printing any variable:-
System.out.println(listOfCompanies);

And the output of the toString method overridden will be printed to the console for all the instances added to the list.

明月松间行 2024-12-23 20:25:46

如果你的java版本支持varargs那么你可以这样做:

private static String[] createDynamicStringArray(String... items){
  return items;
}

usage:

String[] items = createDynamicStringArray("item1", "item2", "item3");

但我建议重新考虑你的设计。

If your java version supports varargs then you can do:

private static String[] createDynamicStringArray(String... items){
  return items;
}

usage:

String[] items = createDynamicStringArray("item1", "item2", "item3");

But I would suggest to rethink your design.

明媚殇 2024-12-23 20:25:46

您应该使用列表代替。数组不支持此功能。

请谷歌一下 Java 中列表相对于数组的优点

You should use List instead. Arrays doesnot support this feature.

Please google Advantages of List over arrays in Java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文