2 Replies Latest reply: Feb 27, 2013 2:48 AM by gimbal2 RSS

    How to use an Arraylist of Int arrays

    HawkerHunter
      I have to capture arrays of size two within another array like {[1,2],[2,3],[3,4],............,[(n-1),n]}
      I can set the size of the second array to two but the size of the main array shall be calculated at run time.
      So I decided to take the main array as an Array List so that size can dynamically increase.

      I would like to know how to enter the elements to the secondary array and how to add the secondary arrays to the main array list.
      Also I would like to know how to read the elements of the secondary or internal array and compare with another elements of the secondary array.

      Regards
      Hawker Hunter
        • 1. Re: How to use an Arraylist of Int arrays
          Kayaman
          If you know the arrays are always going to be size two, I'd recommend creating a Pair class to hold the variables (it's annoying that the SDK doesn't have a ready made one).
          It's cleaner that way, even though it's possible to do with arrays too.

          As for your question about basic usage of the ArrayList class, I refer you to the javadoc and the numerous examples on the internet.
          • 2. Re: How to use an Arraylist of Int arrays
            gimbal2
            HawkerHunter wrote:
            I have to capture arrays of size two within another array like {[1,2],[2,3],[3,4],............,[(n-1),n]}
            I can set the size of the second array to two but the size of the main array shall be calculated at run time.
            So I decided to take the main array as an Array List so that size can dynamically increase.

            I would like to know how to enter the elements to the secondary array
            First obtain the array from the arraylist with its get() method, then index into it like you normally would index into an array
            and how to add the secondary arrays to the main array list.
            With the list add() method

            http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
            Also I would like to know how to read the elements of the secondary or internal array and compare with another elements of the secondary array.
            Use two loops? Hard to give a specific answer to such a non-specific requirement.