Re: [Vala] List object or ArrayList



Joss 4 wrote:
 Hi Yu

I forget to say that I had problem with List object.  I mean. I confused
List with ArrayList even though I seen that ArrayList inherit from List.
I need a container where save all the lines of a file and than order it
in alphabetical order.

so I created a test code like this :

List<string>  list  = new List<string>();

list.Append("uno");
list.Append("due");
list.Append("tre");
list.Append("quattro");

list.Sort(Gtk.strcmp);

with this code :
1. I wrong defining List in place of ArrayList, but :
2- This code doesn't compile at line   * List.Sort(Gtk.strcmp); * I
don't know why.

Hi Joss,

there are two issues with your code:

- the method names must start with lower case.
- 'strcmp' is in namespace GLib, not Gtk.
  (GLib is always imported implicitly by Vala)

This code should work:

--------------------------------------------------------
void main () {
    List<string> list = new List<string> ();
    list.append ("uno");
    list.append ("due");
    list.append ("tre");
    list.append ("quattro");
    list.sort (strcmp);
    stdout.printf ("%s\n", list.nth_data (0));
    foreach (string pi in list) {
        stdout.printf ("%s\n", pi);
    }
}
--------------------------------------------------------


Best Regards,

Frederik



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]