public static void main(String args[]) {
myMethod(); // i am calling static method from main()
}
.Restituisce diversi tipi di dati da un metodo in java?
public static ? myMethod(){ // ? = what should be the return type
return value;// is String
return index;// is int
}
myMethod()
torneranno String e int value. Quindi prendi questi valori di ritorno da main()
ho trovato la seguente soluzione.
creare una classe chiamata ReturningValues
public class ReturningValues {
private String value;
private int index;
// getters and setters here
}
e cambiare myMethod()
come segue.
public static ReturningValues myMethod() {
ReturningValues rv = new ReturningValues();
rv.setValue("value");
rv.setIndex(12);
return rv;
}
Ora la mia domanda, c'è un modo più semplice per raggiungere questo obiettivo?
Si potrebbe usare 'Proprietà' o' HashMap' o anche 'Elenco', ma io il tuo' ReturnValues' è più appropriato in quanto non è ambiguo su cosa restituirà il metodo – MadProgrammer
Come sono legati all'indice e al valore? –