Trouble displaying results from object with brackets, web service example
I have created a JAX-WS client within eclipse that will communicate with a web service that was written in VB.net. I have gotten this to work successfully.
One of my web service methods will return an obect of type KitchenItems[]
The KitchenItems has a bunch of get/set methods for various kitchen properties. H开发者_如何学编程owever, I cannot access those methods when using KitchenItems[]
Do the brackets convert the object into an array? How can I gain access to the get methods in KitchenItems? I had a test class automatically generated which did the following in order to extract the results:
KitchenItem[] kitchenItem= soap.getKitchenItemsByLoginId(kitchenId);
List list = Arrays.asList(kitchenItem.);
String result = list.toString();
Ideally, I would like to work with the object, and not convert to a string. The above won't let me convert the array to as a list anyway, as it throws the error:
Type mismatch: cannot convert from List<KitchenItem> to List
Any suggestions? Thanks!
Square brackets on the variable's type, KitchenItem[]
, or on the variable's name, kitchenItem[], indicate that this variable is an array.
The elements of an array are accessed with the square brackets operator where the first element is accessed as kitchenItem[0]
and arrays have a length
field for determining the number of elements in the array.
Assume we have the array of items:
KitchenItem[] kitchenItems = soap.getKitchenItemsByLoginId(kitchenId);
To print the name of the first element in the array:
System.out.println(kitchenItems[0].getName());
To print the name of the last element in the array:
System.out.println(kitchenItems[kitchenItems.length - 1].getName());
One could print all the name of each KitchenItem
with this code:
KitchenItem[] kitchenItems = soap.getKitchenItemsByLoginId(kitchenId);
for (int i = 0; i < kitchenItems; i++) {
KitchenItem kitchenItem = kitchenItems[i];
System.out.println(kitchenItem.getName());
}
The array can be turned into a genericized List
using
List<KitchenItem> list = Arrays.asList(kitchenItem);
However the code you have may not be working because of the extra .
on the end or because you have strict checking on in Eclipse. A genericized List
should be assignable to a simple List
.
精彩评论