swarm-support
[Top][All Lists]
Advanced

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

Re: I: printing lists (java)


From: Paul E Johnson
Subject: Re: I: printing lists (java)
Date: Tue, 27 Mar 2001 13:22:30 -0600

Matteo Richiardi wrote:
> 
> Hello Paul,
> could you please confirm me you have received my previous mail? If you don't
> have time to answer, I will send it to the list...
> 
> best regards,
> 
> Matteo Richiardi

OK, i'm looking at the code you emailed me. Have you still got problems?

I see you want to have a map, which includes lists as objects.  THen you
want to print out summaries individually for the lists.  

If you are using Swarm in Java, it is probably better to do all this
stuff with Java collections classes, not Swarm obj-c stuff through the
native interface. If it is really important to you to keep working with
the Swarm collections, we can do it.  I pasted below a working version
of part of your program, the second part I did not understand your
objective at all, sorry.

One mistake in your code is the scope of klist, another, I think, is the
usage of next on a map index. The next gives you the next object, not
the next key.  If you are using a map index, use getKey to get the
current key. I show how below.  There are some differences between the
objective C way I learned and the java API, so I don't want to say too
much more.  

You had a mistake in the bounds on a for loop, which is a commmon
mistake, easy to fix, in your printList method. I commented that out
because it was not clear to me the purpose of it, but if you see how
this runs now, perhaps you can revise again and tell us what you get.

Note, I'm not implying this program is logical. You insert the same
object twice in list2 over and over, so the printout from the run shows
duplicates.

Good luck

/**
 * PrintMaps.java
 *
 *
 * Created: 11/03/2001
 *
 * @author Matteo Richiardi
 * @version 0.1
 */


import swarm.defobj.Zone;
import swarm.collections.List;
import swarm.collections.Map;
import swarm.collections.MapImpl;
import swarm.collections.MapIndex;
import swarm.collections.ListImpl;
import swarm.collections.ListIndex;
import swarm.Globals;
import swarm.Selector;


public class PrintMaps  {
    static MapImpl map;
    public static void main(String[] args) {
        Globals.env.initSwarm("PrintMaps", "2.1.1","address@hidden", args);
        ListImpl list1, list2;
        map=new MapImpl(Globals.env.globalZone);
        list1=new ListImpl(Globals.env.globalZone);
        list2=new ListImpl(Globals.env.globalZone);
        for (int n=10;n<20;n++){
            Integer nObj=new Integer(n);
            list1.addLast(nObj);
            System.out.println(+n);
        }
        for (int m=20;m<30;m++){
            Integer mObj=new Integer(m);
            list2.addLast(mObj);
            System.out.println(+m);
            list2.addLast(mObj);
        }
        map.at$insert (new Integer (1),list1);
        map.at$insert (new Integer (2),list2);


        //  // First method:
        MapIndex mapIndex = map.mapBegin (Globals.env.globalZone);
        Integer keyObj; 
        ListImpl aListObject;

        while ((aListObject = (ListImpl) mapIndex.next ()) != null) {
            keyObj = (Integer) mapIndex.getKey();
            System.out.println("key: "+((Integer)keyObj).intValue()+" Elements:
" + aListObject.getCount() );
            //ListImpl kList=(ListImpl)map.at(keyObj);
            //ListIndex listIndex=kList.listBegin(Globals.env.globalZone);
            ListIndex listIndex=aListObject.listBegin(Globals.env.globalZone);
            Object kiObj;
            while((kiObj=listIndex.next())!=null){
                System.out.println(+((Integer)kiObj).intValue());
            }
        }


        //pj this does not make sense to me at all!
        //  // Second method:
//      Selector sel;
//      ListImpl kList;
//      try {
//          sel = new Selector(this, "printList", false);
//      } catch(Exception e) {
//          System.err.println("Exception printList: "+e.getMessage());
//          System.exit(1);
//      }  
//      map.forEachKey(kList, sel);
//      return;
    }
//      static void printList(ListImpl kList){
//      for(int i=0; i < kList.getCount();i++){
//          Integer kiObj = kList.atOffset(i);
//          System.out.println(kiObj.intValue());
//      }
//      }
}// StartFirms



-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]