2

I have a small issue when I run into while using arraylists in Java. Essentially I am hoping to store an array in an arraylist. I know that arraylists can hold objects, so it should be possible, but I am not sure how.

For the most part my arraylist (which is parsed from a file) is just holding one character as a string, but once in a while it has a series of characters, like this:

    myarray
0    a
1    a
2    d
3    g
4    d
5    f,s,t
6    r

Most of the time the only character I would care about in the string residing at position 5 is the f but occasionally I may need to look at the s or the t as well. My solution to this is to make an array like this:

      subarray
0     f 
1     s
2     t

and store subarray in position 5 instead.

    myarray
0    a
1    a
2    d
3    g
4    d
5    subarray[f,s,t]
6    r

I tried to do this with this code:

 //for the length of the arraylist
 for(int al = 0; al < myarray.size(); al++){
      //check the size of the string
      String value = myarray.get(al);
      int strsz = value.length();
      prse = value.split(dlmcma);
      //if it is bigger than 1 then use a subarray
      if(strsz > 1){
          subarray[0] = prse[0];
          subarray[1] = prse[1];
          subarray[2] = prse[2];
      }
      //set subarray to the location of the string that was too long
      //this is where it all goes horribly wrong
      alt4.set(al, subarray[]);
  }

This isn't working the way I would like though. It won't allow me to .set(int, array). It only allows .set(int, string). Does anyone have suggestions?

2
  • what is alt4 (object declaration)? Commented Oct 17, 2012 at 21:03
  • You do realize that a String is a char[] with methods around, right? Just use a List<String> with values like "a" or "fst", there's no need for separators and splitting if what you're interested in is individual characters. Commented Oct 17, 2012 at 21:37

5 Answers 5

2

The easiest approach would be to have an ArrayList of ArrayList.

ArrayList<ArrayList<String>> alt4 = new ArrayList<ArrayList<String>>();

However, this probably isn't the best solution. You may want to rethink your data model and look for a better solution.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah. I will be rethinking it today. Thanks
0

Just change alt4.set(al, subarray[]); to

         alt4.add(subarray);

I assume alt4 is another defined ArrayList<String[]>. If not, define it as below:

        List<String[]> alt4= new ArrayList<String[]>();

or

        ArrayList<String[]> alt4= new ArrayList<String[]>();

Comments

0

My guess is that you are declaring alt4 as List<String> and that's why it is not letting you set an array of String as a list element. You should declare it as List<String[]> and is each element is only singular, simply set it as the 0th element of the String[] array before adding it to the list.

Comments

0

You could switch to:

List<List<Character>> alt4 = new ArrayList<List<Character>>();  

Comments

0

May be this is what you want to get

public class Tester {

    List<String> myArrays = Arrays.asList(new String[] { "a", "a", "d", "g", "d", "f,s,t", "r" });

    ArrayList<ArrayList<String>> alt4 = new ArrayList<ArrayList<String>>();

    private void manageArray() {
        // for the length of the arraylist
        ArrayList<String> subarray = new ArrayList<String>();
        for(int al = 0; al < myArrays.size(); al++) {
            // check the size of the string
            String value = myArrays.get(al);
            int strsz = value.length();
            String prse[] = value.split(",");
            // if it is bigger than 1 then use a subarray
            if(strsz > 1) {
                for(String string : prse) {
                    subarray.add(string);
                }
            }
            // set subarray to the location of the string that was too long
            // this is where it all goes horribly wrong
            alt4.set(al, subarray);
        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.