1 2 Previous Next 24 Replies Latest reply: Sep 11, 2007 6:43 AM by 796254 RSS

    how can i create a 2D vector??

    807587
      Hi,
      I am doing a client-server application which is simliar to ICQ.

      And now i want to create a 2D vector.
      i tried:
      Vector [] [] v=new Vector();
      but it doesn't work, i want to place the loginID and IP address of clients into a vector and send this vector as an object to all clients. In the client application, i will make use of this vector to construct an online list by using JList.
      could somebody please give me some ideas??thanks!
        • 1. Re: how can i create a 2D vector??
          807587
          If you want a Vector containing Vectors then you just declare one Vector and then add others too it:
          Vector list = new Vector();
          Vector one = new Vector();
          one.add(loginId);
          one.add(ipAddress);
          list.add(one);
          For your example though, you'd be better off creating a class to represent a login with id and IP address properties and storing a list of these.
          • 2. Re: how can i create a 2D vector??
            807587
            thank you very much!!!
            i love your example!!!!!!!!
            • 3. Re: how can i create a 2D vector??
              807587
              For the record, you completely ignored his advice, which was correct. You don't want to use
              nested vectors to do this, you want a single ArrayList of objects which contain both variables.
              • 4. Re: how can i create a 2D vector??
                807587
                oh thanks!! so it would be better to use a 2D arraylist to get this job done?
                • 5. Re: how can i create a 2D vector??
                  807587
                  No, it would be better to define a class that holds bundles of data (such as login ID and IP address) and then have a 1-D ArrayList of objects of that class.
                  • 6. Re: how can i create a 2D vector??
                    807587
                    should be a good idea!!!
                    but i am new to java and may not be capable of doing this!!
                    thanks for your advice !!!!
                    people here are very helpful!!!
                    • 7. Re: how can i create a 2D vector??
                      807587
                      import java.net.InetAddress;
                      
                      public class IcqUser {
                          private final InetAddress ip;
                          private final String userId;
                      
                          public IcqUser(InetAddress ip, String userId) {
                              this.ip = ip;
                              this.userId = userId;
                          }
                      }
                      • 8. Re: how can i create a 2D vector??
                        807587
                        thanks!!!!i will try!!!!!


                        • 9. Re: how can i create a 2D vector??
                          807587

                          You may want to cut back on the caffeine.
                          • 10. Re: how can i create a 2D vector??
                            JosAH
                            You may want to cut back on the caffeine.
                            One way or another I do appreciate his combination of ignorance, enthusiasm and the
                            abundance of exclamation marks. ;-) we should give him a chance ...

                            kind regards,

                            Jos
                            • 11. Re: how can i create a 2D vector??
                              807587
                              hi,
                              i still have some problems!!
                              could you help me??
                              thanks in advance!

                              in the server application,
                              once a user connects to the server, his ID and IPAddress will be passed into the Users(InetAddress ip, String userid) constructor!!! An object will then be created and i put it into an ArrayList. I want to send the ArrayList using ObjectOutputStream. There was no problem when I complied the server application.
                              Socket s=null;
                              ArrayList ServerOnlinelist=new ArrayList();
                              ..............................................
                              Users aUser=new User(socket.getInetAddress(),loginid);
                              ServerOnlinelist.add(aUser);
                              ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                              oos.write(aUser);
                              oos.flush();
                              oos.close();
                              s.close();
                              
                              class Users {    
                                      private final InetAddress ip;   
                                      private final String userId;    
                                      public Users(InetAddress ip, String userId) {        
                                          this.ip = ip;        
                                          this.userId = userId;    }
                                  }
                              However, in the client application, it can't receive the ArrayList object sent by the server application.
                              What's wrong with my codes??
                              ..........................................
                              Socket socket=null;
                                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                                                   SMI.OnlineList=(ArrayList)ois.readObject();
                                                   System.out.println(SMI.OnlineList.toString());
                                                    ois.close();
                              socket.close();
                              ............................................
                              the following error msgs were from the client application( when i tried to connect to the server):

                              http://hk.geocities.com/iamsupersuperman/AAA.JPG




                              • 12. Re: how can i create a 2D vector??
                                JosAH
                                For one thing: you're not sending the ArrayList, you're just attempting to send one single Users object
                                through that socket, but that's not the point. Look at the very first line of that exception blabber in the
                                jpeg. It says that an object is not serializable. An object is serializable if:

                                1) it (in)directly implements the (empty) Serializable interface;
                                2) all its members are Serializable.

                                Your Users class doesn't implement this empty interface, hence the exception blabber.
                                Lucky for you, both the InetAddress and String members do implement this interface, so all you have
                                to change is this:
                                class Users implements Serializable {
                                kind regards,

                                Jos
                                • 13. Re: how can i create a 2D vector??
                                  807587
                                  thanks first!!!!! i have added the "implements Serializable" !!!
                                  but this time, some new error msgs appear:
                                  http://hk.geocities.com/iamsupersuperman/BBB.JPG

                                  what's wrong????
                                  • 14. Re: how can i create a 2D vector??
                                    JosAH
                                    what's wrong????
                                    Oh my oh my, I think reading back-traces have become an ancient art form ... Look at the topmost line
                                    of all that gibberish: it tells you that a class could not be found, your Users class that it.

                                    Suppose you send an object (your Users object) from computer A to computer B. Computer A knows
                                    where to find your Users class, otherwise it couldn't send it and it couldn't even have created such an
                                    object. The serialization mechanism is smart, i.e. it not just sends all that binary stuff that makes
                                    up the member variable values of that object to computer B, it also sends 'meta information', describing
                                    the class name of the object itself; in your case it sends "Users" to computer B, because computer B
                                    has to re-construct that Users object for you, it can't just simply guess ...

                                    For one reason or another, computer B can't find that class; most likely your Users.class file is not
                                    in the CLASSPATH of computer B.

                                    kind regards,

                                    Jos
                                    1 2 Previous Next