Array in ArrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Unite
    New Member
    • Mar 2008
    • 3

    Array in ArrayList

    I have a ArrayList which I add a Array to. How will I access the array from the ArrayList?

    Example :

    /*Storing the array*/
    String[] Container = new String[3];
    ArrayList arr =new ArrayList();
    ResultSet rec1 = st.executeQuery ("SELECT * FROM HistoryHeader WHERE DocumentDate BETWEEN '"+startDate +"' AND '"+endDate+"'") ;
    while(rec1.next ()) {
    Container[0] = rec1.getString( "DocumentType") ;
    Container[1] = rec1.getString( "DocumentNumber ");
    Container[2] = rec1.getString( "CustomerCode") ;
    arr.add(Contain er);
    }

    /*Viewing the String Array from the Array list*/
    I want to access the array like arr.get(1)[1]
    So get element 1 from the stored array from the first ArrayList element.

    Whats the syntax for this?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your code is so wrong: you load the content of each record (three columns) in
    the same Container over and over again and you add that Container to your list
    over and over again so you end up with n copies of your Container array in your
    list and it contains the data from the last row read from your database. (you have
    overwritten any previous content in that Container array).

    But there is more: why don't you build a simple class that represents the data
    from a single row? Construct an object from that class for each row read and
    add the objects for your list.

    kind regards,

    Jos

    Comment

    • jingles
      New Member
      • Mar 2008
      • 2

      #3
      Originally posted by Unite
      I have a ArrayList which I add a Array to. How will I access the array from the ArrayList?

      Example :

      /*Storing the array*/
      String[] Container = new String[3];
      ArrayList arr =new ArrayList();
      ResultSet rec1 = st.executeQuery ("SELECT * FROM HistoryHeader WHERE DocumentDate BETWEEN '"+startDate +"' AND '"+endDate+"'") ;
      while(rec1.next ()) {
      Container[0] = rec1.getString( "DocumentType") ;
      Container[1] = rec1.getString( "DocumentNumber ");
      Container[2] = rec1.getString( "CustomerCode") ;
      arr.add(Contain er);
      }

      /*Viewing the String Array from the Array list*/
      I want to access the array like arr.get(1)[1]
      So get element 1 from the stored array from the first ArrayList element.

      Whats the syntax for this?

      You are on the right lines. However, the problem is that when you retrieve your String[] from your ArrayList using the .get(index) method, your String[] will be returned as an Object. If you are using an IDE like Eclipse or NetBeans it should tell you the type of the object that is being returned.

      You have two options to solve this problem;

      1. Cast the object being returned back to a String[].

      So for example:
      Code:
      String[] myArray = (String[])arr.get(1);
      System.out.println(myArray[0]);
      If you choose this approach, you should ensure that checks are made that the object being returned is actually a String[] before you get it, otherwise exceptions will be thrown when you recast the object. You can do this using the "instanceof " operator.


      2. Make use of the ArrayList class' generics feature.

      Many of the classes in the java.util package are "generic". This means that you can specify the type of object that they deal with. They will then provide type safety when you are adding and retrieving objects from the class. You can specify an ArrayList to contain String[]'s like so:

      Code:
      ArrayList<String[]> myArrayList = new ArrayList<String[]>();
      The type in between the < and > symbols specifies the type the ArrayList can handle. Once you have an instance of an ArrayList of this specification; your original code will work:

      Code:
      arr.get(1)[1];
      This is because the get(index) method now knows that it should return the type of String[] rather than Object.

      Generics are very useful. You can read more about them here .

      Comment

      • Unite
        New Member
        • Mar 2008
        • 3

        #4
        You sir are my hero. Worked like a charm. Thank you very much.

        Originally posted by jingles
        You are on the right lines. However, the problem is that when you retrieve your String[] from your ArrayList using the .get(index) method, your String[] will be returned as an Object. If you are using an IDE like Eclipse or NetBeans it should tell you the type of the object that is being returned.

        You have two options to solve this problem;

        1. Cast the object being returned back to a String[].

        So for example:
        Code:
        String[] myArray = (String[])arr.get(1);
        System.out.println(myArray[0]);
        If you choose this approach, you should ensure that checks are made that the object being returned is actually a String[] before you get it, otherwise exceptions will be thrown when you recast the object. You can do this using the "instanceof " operator.


        2. Make use of the ArrayList class' generics feature.

        Many of the classes in the java.util package are "generic". This means that you can specify the type of object that they deal with. They will then provide type safety when you are adding and retrieving objects from the class. You can specify an ArrayList to contain String[]'s like so:

        Code:
        ArrayList<String[]> myArrayList = new ArrayList<String[]>();
        The type in between the < and > symbols specifies the type the ArrayList can handle. Once you have an instance of an ArrayList of this specification; your original code will work:

        Code:
        arr.get(1)[1];
        This is because the get(index) method now knows that it should return the type of String[] rather than Object.

        Generics are very useful. You can read more about them here .

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Unite
          You sir are my hero. Worked like a charm. Thank you very much.
          If you go this path you end up with a very non object oriented solution. Have you
          tested your 'solution' with multiple arrays yet?

          kind regards,

          Jos

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by JosAH
            If you go this path you end up with a very non object oriented solution. Have you
            tested your 'solution' with multiple arrays yet?

            kind regards,

            Jos
            I agree with Jos. Your code smell is called "object denial". You need to start defining classes that represent your data.

            Comment

            • karthickkuchanur
              New Member
              • Dec 2007
              • 156

              #7
              Originally posted by Unite
              I have a ArrayList which I add a Array to. How will I access the array from the ArrayList?

              Example :

              /*Storing the array*/
              String[] Container = new String[3];
              ArrayList arr =new ArrayList();
              ResultSet rec1 = st.executeQuery ("SELECT * FROM HistoryHeader WHERE DocumentDate BETWEEN '"+startDate +"' AND '"+endDate+"'") ;
              while(rec1.next ()) {
              Container[0] = rec1.getString( "DocumentType") ;
              Container[1] = rec1.getString( "DocumentNumber ");
              Container[2] = rec1.getString( "CustomerCode") ;
              arr.add(Contain er);
              }

              /*Viewing the String Array from the Array list*/
              I want to access the array like arr.get(1)[1]
              So get element 1 from the stored array from the first ArrayList element.

              Whats the syntax for this?
              try it
              arrayFetch.toAr ray(arrFinance) ;

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by karthickkuchanu r
                try it
                arrayFetch.toAr ray(arrFinance) ;
                What object or class is 'arrayFetch'? What does its 'toArray' method do? What is
                the 'arrFinance' variable? Are you sure you put your reply in the correct thread?

                kind regards,

                Jos

                Comment

                Working...