# Java Project Trouble: Searching one ArrayList with another ArrayList

**807591**Apr 13, 2008 8:11 PM

Hey,

To start out, basically, our program is supposed to fill up two ArrayLists with integers using 4 bytes from a binary file, then sort one list ascendingly, then search for the numbers contained in the second list in the first list, and ultimately display how many were found. I think there is something wrong with my searchList method.

Basically, the first arraylist is filled with {11, 2, 5, 1, 12, 3, 12, 7} and once I sort it, it becomes {1, 2, 3, 5, 7, 11, 12, 12}. The second arraylist (which doesnt have to be sorted) contains {2, 5, 12, 4}. Using my searchList method (which takes in the two arraylists as parameters) I am getting the wrong answer. It is saying that 4 of the 4 in arraylist2 are contained in arraylist1; when actually, only 3 of the 4 is in the first list.

My searchList method looks like this:

-------------------------------------------------------------------------------------------------------------------------------------------------

public String searchList(ArrayList first, ArrayList second)

{

long timeAtBeg = System.nanoTime();

int firstSize = first.size();

int secondSize = second.size();

while(current2 < secondSize)

{

Integer secondTemp = (Integer) second.get(current2);

int numberInSecondList = secondTemp.intValue();

for(int i=0; i<firstSize; i++)

{

Integer firstTemp = (Integer) first.get(i);

int numberInFirstList = firstTemp.intValue();

if(numberInFirstList == numberInSecondList)

{

numberContained++;

current2++;

searchList(first, second);

}

}

current2++;

searchList(first, second);

}

long timeAtEnd = System.nanoTime();

long timeTaken = timeAtEnd - timeAtBeg;

return ("The second list contained " + numberContained + " of the same

integers as the first list." + "\n" + "It took " + timeTaken + " nanoseconds to

find those " + numberContained + " ints.");

}

-------------------------------------------------------------------------------------------------------------------------------------------------

I couldnt figure out what it was doing, cause basically, its picking up the first number in the second list, searching the entire first list till it finds a match, and then moving on through till the end of the second list.

I did some trial and error by sticking in some print statements in the loops to see exactly what integers its comparing. I put a * next to the numbers in the second list, and a -- next to the integers from the first list...I came up with this...

The first list contains 8 integers.

The second list contains 4 integers.

The sort of the first list took 44977 nanoseconds.

2* <----first number of the second list

1-

2-

5* <-----second

1-

2-

3-

5-

12* <-----third

1-

2-

3-

5-

7-

11-

12-

4* <----last

1-

2-

3-

5-

7-

11-

12-

12-

12-

7-

11-

12-

12-

3-

5-

7-

11-

12-

12-

The second list contained 4 of the same integers as the first list.

It took 3044521 nanoseconds to find those 4 ints.

For some reason, it goes through on the first 3 till it finds the number, then moves on, but when it doesnt find the 4, it does something crazy and goes through the list over and under and backwards!!

Anybody know whats happening??

Thanks alot!

To start out, basically, our program is supposed to fill up two ArrayLists with integers using 4 bytes from a binary file, then sort one list ascendingly, then search for the numbers contained in the second list in the first list, and ultimately display how many were found. I think there is something wrong with my searchList method.

Basically, the first arraylist is filled with {11, 2, 5, 1, 12, 3, 12, 7} and once I sort it, it becomes {1, 2, 3, 5, 7, 11, 12, 12}. The second arraylist (which doesnt have to be sorted) contains {2, 5, 12, 4}. Using my searchList method (which takes in the two arraylists as parameters) I am getting the wrong answer. It is saying that 4 of the 4 in arraylist2 are contained in arraylist1; when actually, only 3 of the 4 is in the first list.

My searchList method looks like this:

-------------------------------------------------------------------------------------------------------------------------------------------------

public String searchList(ArrayList first, ArrayList second)

{

long timeAtBeg = System.nanoTime();

int firstSize = first.size();

int secondSize = second.size();

while(current2 < secondSize)

{

Integer secondTemp = (Integer) second.get(current2);

int numberInSecondList = secondTemp.intValue();

for(int i=0; i<firstSize; i++)

{

Integer firstTemp = (Integer) first.get(i);

int numberInFirstList = firstTemp.intValue();

if(numberInFirstList == numberInSecondList)

{

numberContained++;

current2++;

searchList(first, second);

}

}

current2++;

searchList(first, second);

}

long timeAtEnd = System.nanoTime();

long timeTaken = timeAtEnd - timeAtBeg;

return ("The second list contained " + numberContained + " of the same

integers as the first list." + "\n" + "It took " + timeTaken + " nanoseconds to

find those " + numberContained + " ints.");

}

-------------------------------------------------------------------------------------------------------------------------------------------------

I couldnt figure out what it was doing, cause basically, its picking up the first number in the second list, searching the entire first list till it finds a match, and then moving on through till the end of the second list.

I did some trial and error by sticking in some print statements in the loops to see exactly what integers its comparing. I put a * next to the numbers in the second list, and a -- next to the integers from the first list...I came up with this...

The first list contains 8 integers.

The second list contains 4 integers.

The sort of the first list took 44977 nanoseconds.

2* <----first number of the second list

1-

2-

5* <-----second

1-

2-

3-

5-

12* <-----third

1-

2-

3-

5-

7-

11-

12-

4* <----last

1-

2-

3-

5-

7-

11-

12-

12-

12-

7-

11-

12-

12-

3-

5-

7-

11-

12-

12-

The second list contained 4 of the same integers as the first list.

It took 3044521 nanoseconds to find those 4 ints.

For some reason, it goes through on the first 3 till it finds the number, then moves on, but when it doesnt find the 4, it does something crazy and goes through the list over and under and backwards!!

Anybody know whats happening??

Thanks alot!

- 22 Views
- Tags: none (add)