-3

I want to split an array list into smaller separate lists with 6 elements in each of them. For example, a user will input the following information:

HKY SGP 18:00 2 9:00 400

CHN HKY 17:00 3 6:00 500

RRK SSK 19:00 1 7:00 300

These info will be stored into an arraylist. Then i want to separate this arraylist, so that the info will be like this:

List 1: HKY SGP 18:00 2 9:00 400

List 2: CHN HKY 17:00 3 6:00 500

List 3: RRK SSK 19:00 1 7:00 300

This is to compare this lists with another arraylist and extract certain information.

Any help is appreciated and if question is not clear, please do inform me.

Thank you!

Phantom
  • 119
  • 2
  • 11
  • 3
    Probably someone will ask what have you tried.. So prepare an answer.. – Maroun Sep 12 '13 at 15:29
  • 2
    Why to split the array data in 6? Instead create a class that holds each piece of data in a field, so your class will contain 6 fields, then store an instance of this class in your `List`. – Luiggi Mendoza Sep 12 '13 at 15:32
  • @MarounMaroun I have tried using split, but not really sure on how to split into separate small lists. My first time using vector, that's the reason for the confusion. – Phantom Sep 12 '13 at 15:32
  • @LuiggiMendoza Ahh...that sounds like a good idea, will try that out – Phantom Sep 12 '13 at 15:34
  • 1
    don't use `Vector` or `Hashtable` in modern code; use `List` or `Map` implementations. If you have a book or tutorial that says to use `Vector` quit using it and throw it out immediately. –  Sep 12 '13 at 15:42
  • @JarrodRoberson Why is using vector or hashtable a bad idea? – Phantom Sep 12 '13 at 15:47
  • @Phantom they have all be been for all practical purposes *deprecated*, and if anyone sees you using them today, they will assume you don't know anything about modern practices. –  Sep 12 '13 at 15:49
  • @JarrodRoberson Not to be pedantic, but `Vector` is a `List` implementation. Also http://docs.oracle.com/javase/7/docs/api/deprecated-list.html#class. Did the community take it upon themselves to deprecate this API?because it's not officially as of java 7. – Cruncher Sep 12 '13 at 15:53
  • 2
    @Cruncher http://stackoverflow.com/q/1386275/1065197 – Luiggi Mendoza Sep 12 '13 at 15:57
  • @Cruncher be pendantic as you like; when you go to an interview and use `Vector` or `Hashtable` or `Enumerator` on the whiteboard and don't get the job, don't blame anyone but yourself for not understanding why it is not accepted usage anymore and infer ignorance of current best practices. –  Sep 12 '13 at 17:09

2 Answers2

1

You can traverse an array list in groups of n elements using subList(), and then add them to a list of lists:

List<List<String>> output = new ArrayList<List<String>>();
for (int i=0; i < arrayList.size(); i+=6) {
    List<String> group = arrayList.subList(i, i+6);
    output.add(new ArrayList<String>(group));
}

In the end, output will contain a list of six-element lists. That answers the question, but really, why handle all data as lists? it seems like a good idea to encapsulate each group of six values as attributes in a class.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • This sounds right. Sorry, my first time using arraylist, so i am a bit lost. Thank you for the help, will try this out :) – Phantom Sep 12 '13 at 15:35
  • 2
    No problem! but please consider the suggestion at the end - better use a class for storing the data – Óscar López Sep 12 '13 at 15:37
  • Yup, saw the information. I can't believe that i didn't think about that :( Will be trying to implement that idea :) – Phantom Sep 12 '13 at 15:39
0

I think you are wanting a combination of a two dimensional array and using String#split(). The following example should get you pretty close to what you are looking for.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SplitString
{
    private static List<String> listOfFullStrings = new ArrayList<String>();

    static
    {
        listOfFullStrings.add("HKY SGP 18:00 2 9:00 400");
        listOfFullStrings.add("CHN HKY 17:00 3 6:00 500");
        listOfFullStrings.add("RRK SSK 19:00 1 7:00 300");
    }

    public static void main(String[] args)
    {
        String[][] parsedArrays = new String[listOfFullStrings.size()][6];

        for (int i = 0; i < listOfFullStrings.size(); i++)
        {
            parsedArrays[i] = listOfFullStrings.get(i).split(" ");
        }

        for (int i = 0; i < parsedArrays.length; i++)
        {
            System.out.println("List " + i + " :" + Arrays.asList(parsedArrays[i]));
        }
    }
}

The output would be the following:

List 0 :[HKY, SGP, 18:00, 2, 9:00, 400]
List 1 :[CHN, HKY, 17:00, 3, 6:00, 500]
List 2 :[RRK, SSK, 19:00, 1, 7:00, 300]

Note: I convert to a list just to get better formatting when printing it out.

dsingleton
  • 976
  • 6
  • 7