Vinoth
Vinoth

Reputation: 1349

how to make an array dynamically in JAVA?

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 ?

Upvotes: 0

Views: 3370

Answers (5)

whitehat
whitehat

Reputation: 2391

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.

Upvotes: 1

Saurabh
Saurabh

Reputation: 799

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 !

Upvotes: 1

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5533

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

Please google Advantages of List over arrays in Java

Upvotes: 0

Alex Nikolaenkov
Alex Nikolaenkov

Reputation: 2545

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.

Upvotes: 0

r0ast3d
r0ast3d

Reputation: 2635

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.

Upvotes: 1

Related Questions