简体   繁体   中英

How can I work around a list resizing issue when an ArrayList is not an option to use?

We have a search tool that displays results in a table. The list I have to compose and return has to be something like this:

List[] res = new List[(some_int_initializer];
return res;

The problem this poses is that this type of list is not re-sizeable. This problem poses a problem I have, in which I have to resize this list when I don't

    while(collection.iterator().hasNext())
    {
        StringBuilder sb = new StringBuilder();
        sb.append("C:\\test\\sage\\data\\");
        List<String> myList = collection.iterator().next();

        // iterate through string list and compose file paths
        for(String name: myList)
        {
            Matcher matcher = samplePattern.matcher(sampleName);

            if(matcher.find())
            {
                sb.append(matcher.group(1));
                sb.append(matcher.group(2));
                sb.append(matcher.group(3));
                sb.append("000\\nmr\\");
                sb.append(name);
            }

            File file = new File(sb.toString());
            int counter = 0;
            List[] res = new List[myList.size()];

            if(file.exists())
            {
                File[] dirs = file.listFiles();

                for(int step=0;step<dirs.length;step++)
                {
                    List row = new ArrayList();
                    row.add(name);
                    row.add(dirs[step].getAbsolutePath());
                    res[counter++] = row;
                }
            }
        }
    }

The name and path have to be displayed on a row, but a name can have more than one path associated with it. Also, even if the file path does not exist, the name still show in the table. This is make it really difficult to resize the list, especially when I have to add each Array list to 'res'.

Any thoughts or ideas appreciated.

UPDATE

Thanks to all who have responded. This is the solution that worked for me:

 List[] results = allRows.toArray(new List[allRows.size()]);

Thanks to all who have responded. This is the solution that worked for me:

List[] results = allRows.toArray(new List[allRows.size()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM