2D in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nanhiPari
    New Member
    • Jul 2007
    • 8

    2D in Java

    hello..

    making a Project..n want to ask the Guru's of Java..

    can java provide me a method so that i can store multiple phone no's in one variable n this phone nos corresponds to a Address..

    well what i thought was a 2D Array for this..but when i made it, the data was not stored in the variable coz i have not given the SIze(coz it can Varry) of the Array..

    Any solution...??

    Thanz in Advance

    Cheerz!!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by nanhiPari
    hello..

    making a Project..n want to ask the Guru's of Java..

    can java provide me a method so that i can store multiple phone no's in one variable n this phone nos corresponds to a Address..

    well what i thought was a 2D Array for this..but when i made it, the data was not stored in the variable coz i have not given the SIze(coz it can Varry) of the Array..

    Any solution...??

    Thanz in Advance

    Cheerz!!
    Basically an Address can have zero or more PhoneNumbers? Why not just do this:

    [code=java]
    public class PhoneNumber {
    // all the obligatory methods in here
    }
    //
    public class Address {
    private Set<PhoneNumber > phones= new HashSet<PhoneNu mber>();
    // ...
    public boolean addPhoneNumber( PhoneNumber pn) {
    return phones.add(pn);
    }
    public boolean hasPhoneNumber( PhoneNumber pn) {
    return phones.contains (pn);
    }
    // etc.
    }
    [/code]

    kind regards,

    Jos

    Comment

    • nanhiPari
      New Member
      • Jul 2007
      • 8

      #3
      Originally posted by JosAH
      Basically an Address can have zero or more PhoneNumbers? Why not just do this:

      [code=java]
      public class PhoneNumber {
      // all the obligatory methods in here
      }
      //
      public class Address {
      private Set<PhoneNumber > phones= new HashSet<PhoneNu mber>();
      // ...
      public boolean addPhoneNumber( PhoneNumber pn) {
      return phones.add(pn);
      }
      public boolean hasPhoneNumber( PhoneNumber pn) {
      return phones.contains (pn);
      }
      // etc.
      }
      [/code]

      kind regards,

      Jos
      Thankz for the suggestion Jos..but where i m doing this Project(my college..) it has java 1.4 n Generic is not Supported on that version..

      well any other way to do this Thing..??

      Thankz

      Cheerz!!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by nanhiPari
        Thankz for the suggestion Jos..but where i m doing this Project(my college..) it has java 1.4 n Generic is not Supported on that version..

        well any other way to do this Thing..??

        Thankz

        Cheerz!!
        Sure, do it the old-fashioned way with Lists and casts and all. There's no real
        difference except for the verbosity in your code, i.e. the general idea doesn't change.

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          .. and don't forget there is a full page on HashSet in the Java API specs.

          Comment

          • nanhiPari
            New Member
            • Jul 2007
            • 8

            #6
            Sure, do it the old-fashioned way with Lists and casts and all. There's no real
            difference except for the verbosity in your code, i.e. the general idea doesn't change.

            kind regards,

            Jos
            Originally posted by r035198x
            .. and don't forget there is a full page on HashSet in the Java API specs.

            Thanz for the info..

            thanz Jos..will try it that way..n see..whether it works or not in my case...

            cheerz!!

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by nanhiPari
              Thanz for the info..

              thanz Jos..will try it that way..n see..whether it works or not in my case...

              cheerz!!
              Of course it will: suppose you have 'T' type of elements in your list; the Java 1.5
              way would be something like this (using the new for loop):

              [code=java]
              for (T t: list)
              // do something with that 't'
              [/code]

              In 'old' Java 1.4 you'd have to do this:

              [code=java]
              for (int i= 0, n= list.size(); i < n; i++) {
              T t= (T)list.get(i);
              // do something with that 't'
              }
              [/code]

              kind regards,

              Jos

              Comment

              Working...