Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing array to an object in java

Posted on 2007-09-28
11
Medium Priority
?
3,331 Views
Last Modified: 2013-11-24
I have an array of data type Troop[] in java . This array can be empty or may hold multiple values. IF it has any values i need to pass it to an addTz object otherwise it should be ignored if this array is empty or null.

initially the values are stored in a comma separated straing as  String a="abc,def,hey,"

I need to convert these into and array and then pass it to addTz.setvalues(a) or something like that.


How do I do it?

Thanks
0
Comment
Question by:anup001
  • 5
  • 4
  • 2
11 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 19982666
You can convert them to array as
 
String[] array = a.split(",");
0
 

Author Comment

by:anup001
ID: 19984273
and how to I pass this array values to an object that needs an array as an input parameter?

Is the following correct?

the object is addTz.setValues(array);
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 19984290
should be ok if the signature of the function setValues takes array
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:anup001
ID: 19984611
i get this error


The method setValues(Troop[]) in the type addTz is not applicable for the arguments (String[])      
0
 

Author Comment

by:anup001
ID: 19984638
actually looks i need to convert this array into type of Troop[] and then pass it to function addTz. how do i do it?
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 19984652
You need to call appropriate constructor of Troop.
0
 

Author Comment

by:anup001
ID: 19984680
do i initializ Toopr[] as follows?

Troop trp =new Troop();
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 19984786
No, it should be something like this:
 
Troop[] array = new Troop[size]; // size is the size of the array
for(int i=0; i<array.length; i++) {
   array[i] = new Troop(); // Call appropriate constructor.
}
0
 
LVL 5

Expert Comment

by:gksinghiet
ID: 19985111
To store the comma seperated string into array you  you can use split method of String class as:

String a="abc,def,hey,"
String[] sarray = a.split(",");
This gives you String array with three elements, i.e. sarray[0]="abc", sarray[1]="def", sarray[2]="hey"
0
 

Author Comment

by:anup001
ID: 19985186
Thanks Ajay but the size of array depends on the number of comma delimited values which we will get by splitting right? So how do I put the size value during initialization.

 
Troop[] array = new Troop[size]; // size is the size of the array
0
 
LVL 5

Accepted Solution

by:
gksinghiet earned 2000 total points
ID: 19985979
First split the comma seperated string and then pass the length of the restuting array as size parameter.
As:
String[] sarray = a.split(",");
Troop[] array = new Troop[sarray.length];
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question