Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:02:40+00:00 2026-06-10T20:02:40+00:00

I am attempting to loop through some string arraylists to match elements, but I

  • 0

I am attempting to loop through some string arraylists to match elements, but I am getting an error that my arrays are not of the same length. I have one arraylist with a bunch of samples in it and a second with just a few samples. I wish to search through the the first arraylist while comparing and matching them to the values in the second. When I find the places that the two arraylists match I want to take the index of the first and apply it to a third, which holds the means that coordinate with the samples (which are stored in the first, as a reminder.) I have included code leading up to where the problem is, but have tried to keep it as concise as possible. Essentially I am hoping someone could explain either the error I am getting, or a better way to compare them.

//this is how they are declared
ArrayList<String> pit = new ArrayList<String>(); int h =0;
...etc...
//a file is read in
while((sLine=inpt.readLine())!=null){
      splitn=sLine.split(delim2);
      //splits the file into two different ArrayLists, names and means
      pit.add(splitn[0]); pit2.add(splitn[2]);
}
String b="mean"; int pitn = 0;
//remove column titles from those two lists
while(pitn<pit.size()){
     if(pit2.get(pitn).equals(b)){
        pit.remove(pitn); pit2.remove(pitn);
     }
     else{
          ++pitn;
     }
}
//match a pattern to the file names that were entered
ArrayList<String> sampleNum = new ArrayList<String>();          
for(String inp : filenames) {
     Matcher matt=patt.matcher(inp);
     while(matt.find()){
           if(matt.groupCount() >= 2) {
              //match the first part of the file name
              samplenum = matt.group(1);
              //match the second grouping to paneltype
              paneltype = matt.group(2);
           }
           //add sample names to another arraylist
           sampleNum.add(samplenum);
     }
    **//I wish to search through the pit values for a place where it matches sampleNum
    //Problematically I am getting an error
    //for the length of pit** 
     for(int inx=0;inx<pit.size();inx++){
        //if the value of pit equals the value of sampleNum
        if(pit.get(inx).equals(sampleNum.get(h))){
           //add the value, of the same index, from pit2 to the mncov arraylist
           mncov.add(pit2.get(inx));
           h++;
        }
     }

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

I am entering 2 files so this makes sense, because sampleNum is taken from the file names. 2 files = 2 file names

at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at consistentbelow.ConsistentBelow.main(ConsistentBelow.java:**72**) 

Line 72 is this line:

(pit.get(inx).equals(sampleNum.get(h))){

So I am not at all sure what is the problem here. I feel like I am missing something kind of obvious but have mulled on it to the point of blindness. I THINK that I gave enough info to get some help but I wouldn’t bemoan giving some more if it would be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:02:41+00:00Added an answer on June 10, 2026 at 8:02 pm

    I think your problem is not a problem of the size of pit but of the size of sampleNum. You are incrementing h every time you find a match, but have nothing stopping h from incrementing higher than the total length of sampleNum (ie everything has matched and it keeps trying to match). A quick fix may be something like this

    for(int inx=0; inx<pit.size() && h < sampleNum.size(); inx++){
        if(pit.get(inx).equals(sampleNum.get(h))){
           //add the value, of the same index, from pit2 to the mncov arraylist
           mncov.add(pit2.get(inx));
           h++;
        }
    }
    

    Not the most elegant fix but should eliminate the error I believe. I also suspect this might not have exactly the output you are expecting, but hard to say without a better idea of what you are trying to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to loop through an array of numbers, match them to the
I have the following code that I'm attempting to simply loop through an entity
I am attempting to write some code that will expediently process video frames. I
I am attempting to catch the last case in a forEach loop, but all
I'm trying to loop through some static properties in a simple static class in
I am attempting to loop through all of a maps annotations and check to
I am attempting to loop through rows in MySQL with the following code. Unfortunately,
I'm attempting to load numerical data from CSV files in order to loop through
Using Excel Interop, I'm attempting to loop through all QueryTables in all Worksheets and
I'm returning data via JSON and attempting to loop through the JSON dataset to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.