I wrote a function in Java and I want this function to return multiple values. Except use of array and structure, is there a way to return multiple values?
My code:
String query40 = "SELECT Good_Name,Quantity,Price from Tbl1 where Good_ID="+x;
Cursor c = db.rawQuery(query, null);
if (c!= null && c.moveToFirst())
{
GoodNameShow = c.getString(0);
QuantityShow = c.getLong(1);
GoodUnitPriceShow = c.getLong(2);
return GoodNameShow,QuantityShow ,GoodUnitPriceShow ;
}
Java doesn't support multi-value returns.
You can return multiple values by bundling those values into a dictionary, tuple, or a list. These data types let you store multiple similar values. You can extract individual values from them in your main program. Or, you can pass multiple values and separate them with commas.
In Java, when you want a function to return multiple values, you must
In your case, you clearly need to define a class Show
which could have fields name
, quantity
and price
:
public class Show {
private String name;
private int price;
// add other fields, constructor and accessors
}
then change your function to
public Show test(){
...
return new Show(GoodNameShow,QuantityShow ,GoodUnitPriceShow) ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With