0

I currently have an array list named list that contains lots of numbers (doubles). I need to split this big array list into 8 smaller ones and I do this by:

//Array List "list" contains close to 8 million numbers in it
            for (int c1 = 0; c1 < list.size(); c1++)
        { //for
        counter1++;
        if (counter1 % rows ==1)
            ID.add(list.get(c1));
        else if (counter1 % rows ==2)
            Contract.add(list.get(c1));
        else if (counter1 % rows == 3)
            Date.add(list.get(c1));
        else if (counter1 % rows == 4)
            Open.add(list.get(c1));
        else if (counter1 % rows == 5)
            High.add(list.get(c1));
        else if (counter1 % rows == 6)
            Low.add(list.get(c1));
        else if (counter1 % rows == 7)
            Close.add(list.get(c1));
        else if (counter1 % rows == 8)
            Volume.add(list.get(c1));
    }  //for

Every 8th number is the start of a new line of important information.
eg. ID Contract Date Open High Low Close Volume ID1 Contract1 Date1 Open1 High1 Low1 Close1 Volume1

Before i asked if there was a better way to organize this. I now have a DIFFERENT question. Is there a way to search an arraylist outside of using a for loop? instead of organizing the array list like I have (it takes forever with large documents), can I promp a user to enter an ID and Contract and then have my program search for that in the code in a more efficient manner than using a for loop?

3
  • You could use a HashMap where you use the id as the key. Commented Jun 11, 2013 at 14:32
  • Have you thought about using a HashMap docs.oracle.com/javase/6/docs/api/java/util/HashMap.html Commented Jun 11, 2013 at 14:32
  • Use database, probably in-memory. Commented Jun 11, 2013 at 14:33

2 Answers 2

5

Each "line" of important information should be represented by a Java Object, not by a HashMap as some people have suggested in the comments. You're using an Object Oriented programming language, so create a class called Contract (or whatever else represents those 8 fields) and then you will have an ArrayList<Contract> = new ArrayList<Contract>() which will store all of those Objects.

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

2 Comments

Further to the above, if you are mostly accessing it by ID and Contract it might be a good idea to replace the ArrayList<Contract> with a HashMap<String, Contract> where the key is a combination of the ID and the Contract ID.
I agree with Alan. There are also other useful things you can do, such as implementing the Comparable interface, which will allow you to sort your list using Collections.sort(). You can also implement Comparator. There are plenty of articles showing you how to do these things and what the differences are on the net.
0

Since you are using an array already.

The process of splitting will be long, you can't dodge that.

Using Hashmap has mentioned above is good idea has for a key you will have the value in O(1) and not O(n) (array).

BTW you need to take note that every function ( Id.add Contract.add ) could be abstracted and include the code for the data structure you want to use, would it be an hashmap, tree, list or disjoint set. (AS mentioned in the other answer aswell)

On the other hand, to speed up your splitting code modify it to this :

This code reduce the number of modulo to evaluate. Should be faster but maybe not that much.

int selector;
for (int c1 = 0; c1 < list.size(); c1++)
{
    counter1++;
    selector = counter1 = % rows;
    switch(selector)
    {
        case 1:
            ID.add(list.get(c1));
            break;
        case 2:
            Contract.add(list.get(c1));
            break;
        case 3:
            Date.add(list.get(c1));
            break;
        case 4:
            Open.add(list.get(c1));
            break;
        case 5:
            High.add(list.get(c1));
            break;
        case 6:
            Low.add(list.get(c1));
            break;
        case 7:
            Close.add(list.get(c1));
            break;
        case 8:
            Volume.add(list.get(c1));
            break;
    }
}

Here is a faster way considering what I understood from the list content. No evaluation (if/switch/modulo), only getters, less iterations, no counter.

//The list is made of block of 8 units, lets read block by block and not node by node
for (int c1 = 0; c1 < list.size()/8; c1++)
{
    ID.add(list.get(c1*8 + 1));
    Contract.add(list.get(c1*8 + 2));
    Date.add(list.get(c1*8 + 3));
    Open.add(list.get(c1*8 + 4));
    High.add(list.get(c1*8 + 5));
    Low.add(list.get(c1*8 + 6);
    Close.add(list.get(c1*8 + 7));
    Volume.add(list.get(c1*8 + 8));
}    

ON The searching in an array with for loop.

99% of the time, if there is a function to do it like list.find() it will be a for loop trew every node. The only case it its not true is when the data structure is made by the language not to be a list. (Hashmap could be framed has an array/list)

1 Comment

PLease tell me if it did help or not, I might have understood the structure wrong.

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.