1 2 Previous Next 16 Replies Latest reply: Mar 7, 2008 5:16 PM by 807601 RSS

    ArrayList filled with Arrays

    807601
      I'm using an ArrayList to hold several arrays within it:
      String pos1_, pos2, pos3_;
      ArrayList codes_ = new ArrayList();
      String n[] = { pos1_, pos2_, pos3_ }; // always 3 values within each array
      codes_.add(n);
      I'm then using 2 FOR loops (1 for the number of elements in the ArrayList, and another for the 3 values within the array):
      for (int i=0; i<codes_.size(); i++) {
        for (int x=0; x<3; x++) {
          // ...
        }
      }
      ...within these 2 loops I want to check each value of the array within the arrayList.
      In PHP, which it what I'm converting from, I could use:
      if ( codes_[x] == "" ) {
      // do something
      }
      *My Question:*
      
      1) How do I make it look at each element in the array from within the ArrayList?
      +     I know I can get the ArrayList item via: codes_.get(i), but how can I check the [x] val?+
      2) Is there an easier way to do this?
      +     I can't use an array vs. the arrayList cause you have to define the number of elements in the array when you declare it; instead of pushing items onto the end of it --which needs to be done.+
      
      Any help would be greatly appreciated! :)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: ArrayList filled with Arrays
          807601
          You want to access the strings via a statement like so:
          codes_.get(i)[x] 
          Something like this might work:
          for (int i=0; i<codes_.size(); i++) {
            for (int x=0; x<3; x++) {
              System.out.print(codes_.get(i)[x] + ", ");
            }
            System.out.println();
          }
          Note: not tested
          1) How do I make it look at each element in the array from within the ArrayList?
          as noted above.
          2) Is there an easier way to do this?
          You could use some of the for-loop variations, but that's just syntactic sugar.

          Good luck.

          Edited by: Encephalopathic on Mar 7, 2008 3:11 PM
          • 2. Re: ArrayList filled with Arrays
            807601
            Arrays make me nervous, especially when you have a list of arrays or an array of lists. What are you modeling? I suspect you are missing the definition of a proper object.
            • 3. Re: ArrayList filled with Arrays
              807601
              I tried:
              String[] bcArray = (String[])bonusCodes_.toArray(new String[bonusCodes_.size()]);
               if ( bcArray[i][x] == "" ) {
                  // do something
              }
              But it doesn't like
              bcArray[i][x]
              :(

              Error: The type of the expression must be an array type but it resolved to String.+
              Which is unfortunate because I want it to be of type String..not of type array..lol
              • 4. Re: ArrayList filled with Arrays
                807601
                I tried what you suggested Encephalpathic:
                codes_.get(i)[x]
                but it gave me error: The type of the expression must be an array type but it resolved to Object
                • 5. Re: ArrayList filled with Arrays
                  807601
                  soulcrazy wrote:
                  I tried what you suggested Encephalpathic:
                  codes_.get(i)[x]
                  but it gave me error: The type of the expression must be an array type but it resolved to Object
                  using generics? You need to do this:
                  List<String[]> codes = new ArrayList<String[]>();
                  http://java.sun.com/docs/books/tutorial/collections/index.html

                  Again, rethink your need for arrays.
                  • 6. Re: ArrayList filled with Arrays
                    807601
                    BigDaddyLoveHandles wrote:
                    Again, rethink your need for arrays.
                    I agree. To the original poster, ignore everything I posted. Think of creating and using a better model as BDLH suggests.
                    • 7. Re: ArrayList filled with Arrays
                      807601
                      So, instead of using an ArrayList filled with Arrays, use an ArrayList filled with Lists?
                      .. I need to be able to access an item via the FOR loops in some fashion,
                      ...[x]
                                                                                                                                                                                                                                                                                                                                                                                                          
                      • 8. Re: ArrayList filled with Arrays
                        807601
                        No, you could use an generic ArrayList of objects of a class, each object holding three Strings.
                        • 9. Re: ArrayList filled with Arrays
                          807601
                          Would you mind giving me an example using the idea I provided? I'm quite new to Java, I spend most of my time with C++ and PHP.
                          • 10. Re: ArrayList filled with Arrays
                            807601
                            soulcrazy wrote:
                            Would you mind giving me an example using the idea I provided? I'm quite new to Java, I spend most of my time with C++ and PHP.
                            Some would mind as it is akin to asking someone to do your homework. You will have to do some independent studying here. But one way to structure it would be:

                            1) create a class whose objects hold 3 strings. They could very well be in stored in an array.
                            2) create getters and setters for these strings.
                            3) create a generic ArrayList<YourClass> to hold objects of this class.

                            and off you go...
                            • 11. Re: ArrayList filled with Arrays
                              807601
                              Thanks for your input...although it didn't help at all, I do appreciate your attempt.

                              The solution is as simple as:
                                             for (int i=0; i<codes_.size(); i++) {
                                                  for (int x=0; x<3; x++) {
                                                  
                                                       String[] bcArray = (String[])codes_.get(i);                            
                                                          
                                                       if ( bcArray[x] == "" ) {
                                                                              // do something
                                                                      }
                                                              }
                                                       }
                              • 12. Re: ArrayList filled with Arrays
                                807601
                                soulcrazy wrote:
                                Would you mind giving me an example using the idea I provided? I'm quite new to Java, I spend most of my time with C++ and PHP.
                                If you were coding this in C++ I would give you the same nudge: you should define a model-specific class that ends up holding the three strings. Didn't you define classes in C++? Or were you one of those C++ coders?
                                • 13. Re: ArrayList filled with Arrays
                                  807601
                                  I'm not one of those C++ programmers, I'm just lazy at the moment and I wanted something simple and effective, which I have found. Thanks for your concern.
                                  If I could give you a glimpse of the BIG picture, I would..but I'm working with thousands of lines of code and I really didn't need another class for something as simple as this. Ya'll just make it complex cause you're trying to be big-wig guru's.
                                  • 14. Re: ArrayList filled with Arrays
                                    807601
                                    I find that, ultimately, living in object denial and programming in Java but resisting the object-oriented idiom is what complicates code.
                                    1 2 Previous Next