0

I have a problem with the print of my Vector: i have this Vector:

Vector<StructRicetta> Ricetta=new Vector<StructRicetta>();

The class StructRicetta:

public class StructRicetta {
    String nomeric;
    int n;
    Vector<String> ingrediente=new Vector<String>();
    Vector<String> quantita=new Vector<String>();
    Vector<Boolean> facoltativo=new Vector<Boolean>();
    String preparazione;
}

I read the value of this structure from a file. And in the reader function i return to a StructRicetta type and i add it in the vector:

Ricetta.add(Myclass.reader());

Now how can i print the results??

Thank you!

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
MatroxDev
  • 49
  • 8

5 Answers5

2

You're going to have to add a toString() method to your classes, and then iterate through the Vectors.

Perhaps iterate and add each entry to a StringBuilder, and separate using commas or similar ? Apache Commons StringUtils.join() will do this in one call for you.

Note. Vector is largely deprecated these days (it's methods are synchronised by default, which is a little wasteful) and you should look at ArrayList or similar instead.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

Gerenally if you want to print out something useful from an object instead of the memory address, you need to overwrite the public String toString() function, then use System.out.println(that object) to see it.

wizardfan
  • 176
  • 1
  • 7
0

You could add a toString() method to your StructRicetta class, then call

System.out.println(Ricetta);

to print the contents of the Vector to the console.

Qwerky
  • 18,217
  • 6
  • 44
  • 80
0

First i would overide the toString method for your tructRicetta class, than iterate over your Vector like this:

for( StructRicetta rec: Ricetta){
     System.out.println(rec);
}
Michael
  • 2,827
  • 4
  • 30
  • 47
0

Note that Vector implements List, and follows the toString rules for List. Before writing code iterating over the Vector, first test whether its toString meets your requirements.

In order for it to be useful, you will need to give StructRicetta a good toString method.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75