如何在JAVA中动态创建数组?
我的问题很简单,我想创建一个数组并用一些字符串项填充它。下面是一个简单代码,包含两个类和一个主要方法(非常基本的程序)。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用ArrayList,动态添加元素
在您的情况下,它将是 CompanyInput 对象的 ArrayList。
等等...
希望有帮助!
Use an ArrayList, for dynamically adding elements
In your case, it would be an ArrayList of CompanyInput objects.
etc...
Hope that helps !
将您更改
为
对于其他参数,类似地
// 。然后将其添加到 ArrayList 并循环遍历它们并打印它。
Change your
into
// similarly for the other arguments.
And then add it to a ArrayList and loop through them and print it.
在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.
如果你的java版本支持varargs那么你可以这样做:
usage:
但我建议重新考虑你的设计。
If your java version supports varargs then you can do:
usage:
But I would suggest to rethink your design.
您应该使用列表代替。数组不支持此功能。
请谷歌一下 Java 中列表相对于数组的优点
You should use List instead. Arrays doesnot support this feature.
Please google Advantages of List over arrays in Java