Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Indexing ArrayLists of Vectors (AL[i] = new vector();)

Posted on 2007-04-01
5
Medium Priority
?
210 Views
Last Modified: 2010-03-31
Is it possible to index ArrayLists?  I want to have a number of ArrayLists each consisting of a vector....

ArrayList al = new ArrayList();
for (int k=0; k<Bins;k++)
{
   al[k] = new vector();
   for (int l=k*Binsize;l<(k*Binsize)+Binsize;l++)
   {
      al[k].add(Entry[l]);
   }
}

To attempt to clarify the above, Entry[] is an array of integers.  I want to split that array up into 'Bins', each of which consists of a vector of entries from the array.  So, if Entry[6] = {1 2 3 4 5 6), and I want 3 Bins, I want to create 3 ArrayLists consisting of vectors so that al[0] is a vector {1 2}, al[1] is a vector {3 4} and al[2] is a vector {5 6}.  I want to use vectors because I'll be doing a lot of mathematical comparison and merging and vectors/ArrayLists seem like the friendliest animals to manipulate in this way.  

If I haven't confused you to the point of causing you to scream at your monitor and throw things at your pets, I'd greatly appreciate any help.  Thanks.

Jason
0
Comment
Question by:cdromes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18833045
Yes, that's possible. Here is an example with Integers. It uses generics pretty intensive, that's because it's easier to write. I can write the same without generics...

import java.util.*;

public class Example
{
    public static void main(String[] args)
    {
        int[] values = new int[]{1,2,3,4,5,6};

        //Number of bins and bin size
        int binCount = 3;
        int binSize = 2;

        //Bin holder
        ArrayList<Vector<Integer>> all = new ArrayList<Vector<Integer>>();

        //Create bins
        for(int i=0;i<binCount;i++)
        {
            //Create vector
            Vector<Integer> vec = new Vector<Integer>();

            //Fill vector
            for(int k=0;k<binSize;k++)
                vec.add(values[i*binSize+k]);

            //Add new vector
            all.add(vec);
        }

        //Print result
        for(Vector<Integer> vec : all)
        {
            System.out.print("{ ");
            for(Integer val : vec)
                System.out.print(val+" ");
            System.out.println("}");
        }
    }
}

Mark
0
 

Author Comment

by:cdromes
ID: 18833128
Thanks for the quick reply.

In your example, how would I go about accessing the 3rd entry in the 2nd vector in the ArrayList all?  I need to do element-by-element comparisons of neighboring vectors...would something like the following be kosher:

 for (k = 0; k < binCount; k++)
{
      myArrayList.add(new Vector());
      myArrayList.get(k);
      for (l =k*Binsize;l<(k*Binsize)+Binsize;l++)
            myArrayList.get(k).add(work[l]);    
}
Then once you've filled the vectors in the ArrayList, can you compound the .get, to specify vector elements, i.e. myArrayList.get(k).get(vectorElement); ?  
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 700 total points
ID: 18833142
Not sure if the code you posted works, but if it does the job, then it's kosher. :-)

>> Can you compound the .get, to specify vector elements, i.e. myArrayList.get(k).get(vectorElement); ?  

Yes.

Mark
0
 

Author Comment

by:cdromes
ID: 18834054
Spec-friekin'-tacular.  I threw the code in a tested it a couple little files and it worked like a champ.  Thanks, Mark.

Finally, how do I test for the existence of a vector in the ArrayList prior to doing comparison actions?  I'm thinking of something along the lines of

for (int g=0;g<AL.size();g++)
{
      //If this IF is false, then you're at the end of the List
      //You'll have already computed the Chi2 value for this vector, i, with its neighbor, i-1
      if (AL.get(g+1) != null)
      {
      ....
      }
}
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18834653
This will only loop over the vectors inside the AL list.

for (int g=0;g<AL.size();g++)
{
AL.get(g);
}

You can also use an Iterator here:
Iterator iAL = AL.iterator();
while(iAL.hasNext())
{
Vector vec = (Vector) AL.next();
}

Mark
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question