> > Dear all, > > I have a question of List (or java.util.Collection). I have constructed a > Prolog program and a Java interface. The program supposedly generates a list > of predicates for instance "[a(1), b(2), c(3)]", but when I use the Java > interface to do the job by: > ... > CompoundTerm result = program.calculate(query); > ... > > I couldn't get the list, but a Collection of elements. I wonder if there is > a simple way to transfer the Collection back to the Prolog list?? > > Thank you very much. Hello Jordan, Option 1. --------- One way to turn your collection into a Java List (ie java.util.List) would be to simply construct a List passing the collection as an argument. eg. List myList = new LinkedList(collection); Clearly you could use any of the provided list types for this (ArrayList,LinkedList or Vector). Option 2. --------- However you may simply want to iterate through the elements of the collection, in which case an Iterator from the collection will return the elements in the same order as the Prolog list. eg for(Iterator it = collection.iterator(); it.hasNext(); ) { Object element = it.next(); // do something with element } This would avoid needlessly allocating memory for the explicit java.util.List. Option 3. --------- WARNING: This option is NOT recommended as implementation details may change in future releases. If you check the type of the "Collection" object you may find that it is infact a List type. eg. try { List myList = (List)collection; // use the list ... } catch(ClassCastException e) { // oh well, it's not actualy a list after all } Does that help at all, or have a I totally misunderstood the problem? Andrew SadlerReceived on Wed Sep 03 16:32:37 2003
This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:25 PM GMT GMT