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.
I think your problem is not a problem of the size of
pitbut of the size ofsampleNum. You are incrementinghevery 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 thisNot 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.