java - Showing elements of an array of multiple types -
this question has answer here:
- what's simplest way print java array? 23 answers
i need create array following structure:
{int, string, string, int}
i want insert array array this:
{int, string, string, int}, {int, string, string, int}, ... , on.
i have tried this:
object[] vector = new object[100]; public void inserare(int poz, int nr, string nume, string prenume, int nota){ object[] aux = new object[4]; aux[0] = new integer(nr); aux[1] = new string(nume); aux[2] = new string(prenume); aux[3] = new integer(nota); vector[poz] = aux; } public void afisarelista(){ for(int = 0; < vector.length; i++){ system.out.println(vector[i]); } }
aux inserted, when want print elements of main array, this:
[ljava.lang.object;@15db9742
any display correctly elements appreciated.
passing object system.out.println()
results invocation of tostring()
method on passed instance. object
, object
's tostring()
returns address of object , not string
containing field names values.
to address problem :
either create , use custom class contain these 4 fields , populate array instances of class.
override tostring()
in class return expected string.
or else don't rely on tostring()
create utility method return "printable" string of object , invoke in argument of println()
method :
system.out.println(rendermessage(vector[i])); ... public string rendermessage(object[] obj){ return "nr=" + obj[0]+ "nume=" + obj[1]+ "prenume=" + obj[2]+ "nota=" + obj[3]; }
Comments
Post a Comment