Strange selection sort behavior - java

I have tried to attempt implementing my own Selection Sort code. So far, the code sorta works... It just behaves strangely.
class HelloWorld {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int a[] = {
3,7,8,4,22,13,9,5,10
};
int max_pos = a.length-1;
int counter = 0;
int min_sort = 0;
int j = -1;
int min_pos = 0;
int i=0;
while (j < max_pos) {
i=j+1;
min_sort = a[i];
// traverse trough unsorted portion
while (i < max_pos) {
i++;
if (min_sort > a[i]) {
min_sort = a[i]; // minimum unsorted
min_pos = i;
}
}
// sorted portion
j++;
a[min_pos] = a[j];
a[j] = min_sort ;
for(int x=0; x < a.length; x++) {
System.out.print(a[x] + ", ");
}
System.out.print("\t\t");
System.out.println("i: " + i + " j: " + j + " min pos: " + min_pos + " min val: "+ min_sort);
}
}
}
I have tried tracing it, and the output goes like this:
3, 7, 8, 4, 22, 13, 9, 5, 10, i: 8 j: 0 min pos: 0 min val: 3
3, 4, 8, 7, 22, 13, 9, 5, 10, i: 8 j: 1 min pos: 3 min val: 4
3, 4, 5, 7, 22, 13, 9, 8, 10, i: 8 j: 2 min pos: 7 min val: 5
3, 4, 5, 7, 22, 13, 9, 7, 10, i: 8 j: 3 min pos: 7 min val: 7
3, 4, 5, 7, 7, 13, 9, 22, 10, i: 8 j: 4 min pos: 7 min val: 7
3, 4, 5, 7, 7, 9, 13, 22, 10, i: 8 j: 5 min pos: 6 min val: 9
3, 4, 5, 7, 7, 9, 10, 22, 13, i: 8 j: 6 min pos: 8 min val: 10
3, 4, 5, 7, 7, 9, 10, 13, 22, i: 8 j: 7 min pos: 8 min val: 13
3, 4, 5, 7, 7, 9, 10, 13, 22, i: 8 j: 8 min pos: 8 min val: 22

Since this is almost certainly an educational process on your part (real code would just call Arrays.sort()), I won't give a direct answer initially.
Instead, you should notice that it's the line where j == 3 which is when the 8 appears to magically transmogrify into a 7.
So you should start to hone your debugging skills. Run the code through a debugger up to the end of the output of the previous line, then single step every single instruction. At some point, you'll see the 7 become an 8 and that's the code you need to focus on.
Once you've done that, you'll hopefully be able to tell what the problem is. However, if you're still stuck (make sure you try first, or you'll never improve), see below.
t---
This is a detailed analysis of your problem. You are storing information about the minimum (both the value min_sort and its position min_pos) each time you find an element that's smaller than the first one you set up in each iteration of the outer loop:
if (min_sort > a[i]) {
min_sort = a[i]; // minimum unsorted
min_pos = i;
}
Now that's a good way to do it, and these two pieces of information are later used in the swapping process(1):
j++;
a[min_pos] = a[j];
a[j] = min_sort;
However, what of the case where you don't find an element smaller than the first one set up, such as the following where when position 3 holds the value 7 and all values beyond that are greater than 7:
3, 4, 5, 7, 22, 13, 9, 8, 10
^
In that case, the body of the if statement will never be run and the initial conditions from the start of the iteration will still apply:
i=j+1;
min_sort = a[i];
Notice anything missing there? Such as, let's see, the position being set to something?
Bingo! If the first element being checked in an iteration is already in its correct position, min_pos will remain set to whatever it was previously and, when you swap, that's not going to be pretty. You can fix that by changing the code immediately inside the outer while loop to be:
i = j + 1;
min_pos = i; // add this line
min_sort = a[i];
and you'll see the greatly improved (i.e., correct) output of:
3, 7, 8, 4, 22, 13, 9, 5, 10, i: 8 j: 0 min pos: 0 min val: 3
3, 4, 8, 7, 22, 13, 9, 5, 10, i: 8 j: 1 min pos: 3 min val: 4
3, 4, 5, 7, 22, 13, 9, 8, 10, i: 8 j: 2 min pos: 7 min val: 5
3, 4, 5, 7, 22, 13, 9, 8, 10, i: 8 j: 3 min pos: 3 min val: 7
3, 4, 5, 7, 8, 13, 9, 22, 10, i: 8 j: 4 min pos: 7 min val: 8
3, 4, 5, 7, 8, 9, 13, 22, 10, i: 8 j: 5 min pos: 6 min val: 9
3, 4, 5, 7, 8, 9, 10, 22, 13, i: 8 j: 6 min pos: 8 min val: 10
3, 4, 5, 7, 8, 9, 10, 13, 22, i: 8 j: 7 min pos: 8 min val: 13
3, 4, 5, 7, 8, 9, 10, 13, 22, i: 8 j: 8 min pos: 8 min val: 22
(1) The swap, by the way, is not necessary when the smallest remaining value is already in the correct position, though it does no harm. If you wanted to avoid the unnecessary swap, you could use:
j++;
if (min_pos != j) {
a[min_pos] = a[j];
a[j] = min_sort ;
}
However, as stated, it's not a problem to do the swap so it's really up to you.

Related

Java Permutation Assignment

Main class:
import java.util.List;
import java.util.Random;
public class PermutationGenerator{
public static void main(String[] args) {
Random bruh = new Random();
List<Integer> filledArray = nextPermutation.fillArray();
int[] randomArray = new int[10];
for (int i = 0; i < randomArray.length; i++) {
int randomPosition = bruh.nextInt(10 - i) + 1;
randomArray[i] = filledArray.get(randomPosition);
filledArray.remove(randomPosition);
}
System.out.print("List 1: ");
printArray(randomArray);
}
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}
Runner Class:
`import java.util.ArrayList;
public class nextPermutation {
public static ArrayList<Integer> fillArray() {
ArrayList<Integer> randomArray = new ArrayList<Integer>();
randomArray.add(0);
randomArray.add(1);
randomArray.add(2);
randomArray.add(3);
randomArray.add(4);
randomArray.add(5);
randomArray.add(6);
randomArray.add(7);
randomArray.add(8);
randomArray.add(9);
randomArray.add(10);
return randomArray;
}
}
`
I'm supposed to print out something like this:
List 1: 4 6 8 1 9 7 10 5 3 2
List 2: 6 8 1 7 3 4 9 10 5 2
List 3: 2 4 9 6 8 1 10 5 7 3
List 4: 8 5 4 3 2 9 6 7 1 10
List 5: 10 3 2 6 8 9 5 7 4 1
List 6: 9 10 3 2 1 5 6 8 4 7
List 7: 3 8 5 9 4 2 10 1 6 7
List 8: 3 2 4 5 7 6 9 8 10 1
List 9: 4 1 5 10 8 3 6 2 7 9
List 10: 3 5 2 4 1 7 9 6 8 10
But for me, it only prints out one line.
I think I can use a for loop in order to do this. Alternatively, I could just brute force it but I prefer the former.
What I need help on is where and how I should start the for loop.
Any help would be appreciated.
Simply I defined your part of your code in a method mixArray()
Then I added a for loop that runs 10 times to print 10 lists.
The for loop in printArray does run 10 times but it prints 1 number and a space during
each loop so that's why you were getting just 1 line at the end.
for (int p = 1; p <= 10; p++) {
mixArray();
System.out.print("List "+p+": ");
printArray(randomArray);
System.out.println();
}
public static void mixArray() {
for (int i = 0; i < randomArray.length; i++) {
int randomPosition = bruh.nextInt(10 - i) + 1;
randomArray[i] = filledArray.get(randomPosition);
filledArray.remove(randomPosition);
}
}
Also you might wanna keep in mind that your randomizing the array method might give you back a duplicate array. The chances are very low but it is possible.
a lot cleaner to simply use Collections::shuffle
List<Integer> filledArray = fillArray();
for (int i = 0; i < 10; i++)
{
Collections.shuffle(filledArray);
System.out.println(filledArray);
}
output
[7, 0, 3, 5, 2, 1, 9, 10, 4, 8, 6]
[3, 7, 6, 0, 1, 8, 4, 5, 9, 2, 10]
[3, 7, 2, 4, 9, 10, 0, 8, 5, 1, 6]
[8, 1, 0, 7, 4, 2, 3, 6, 10, 9, 5]
[2, 3, 8, 4, 5, 10, 9, 1, 6, 7, 0]
[10, 2, 0, 1, 5, 8, 7, 6, 9, 3, 4]
[1, 5, 0, 9, 2, 4, 6, 8, 10, 7, 3]
[2, 8, 1, 9, 7, 3, 5, 6, 10, 4, 0]
[9, 1, 2, 4, 6, 8, 0, 3, 10, 5, 7]
[4, 5, 2, 3, 10, 9, 6, 8, 0, 1, 7]

Is it possible to check if the 15-puzzle is solvable with a different goal state?

So I have a random 15-puzzle, or any N-puzzle with even width, and I also have a random goal-state. That is, the blank tile and other tiles are also randomly placed.
I am able to check if the 15 puzzle is solvable with the standard goal state of having the tiles in order and blank in the bottom right, but randomizing the goal state seems to be more tricky than the standard 8 puzzle.
Example:
Start State
8 4 1 6
11 2 3 10
15 12 0 9
14 5 7 13
Goal State:
11 4 1 0
8 2 3 10
5 15 6 9
12 9 7 13
The rules for the standard 15-puzzle solvabilty are:
If the width is odd, then every solvable state has an even number of inversions.
If the width is even, then every solvable state has
An even number of inversions if the blank is on an odd numbered row
counting from the bottom;
An odd number of inversions if the blank is on an even numbered row
counting from the bottom;
I don't think different goal have any effect on solvability.
The simplest solution I can think of is to map numbers in the custom goal state to the numbers in the standard goal state. E.g: For the first row you treat 11 as if it was 0, 4-> 1, 1->2, 0->3 and so on.
The algorithm is the same as for standard puzzle, but you need a little change.
In general, the algorithm for reachability from start position S to goal position G is:
Calculate number of inversions in G - I(G)
Calculate number of inversions in S - I(S)
If:
width is odd and I(G) and I(S) have the same parity, G is reachable from S (in other words, S is solvable)
width is even:
Find the row number where blank is located in G - B(G)
Find the row number where blank is located in S - B(S)
If I(G)
is even and I(S) and abs(B(G) - B(S)) have the same parity, G is reachable from S
is odd and I(S) and abs(B(G) - B(S)) have different parity, G is reachable from S
In other cases G is unreachable from S.
Now let's see on your example. Represent a state as a list:
List<Integer> start = Arrays.asList(
8, 4, 1, 6,
11, 2, 3, 10,
15, 12, 0, 9,
14, 5, 7, 13);
List<Integer> goal = Arrays.asList(
11, 4, 1, 0,
8, 2, 3, 10,
5, 15, 6, 9,
12, 14, 7, 13); // in your example there's a second 9 instead of 14
A function, which will count number of inversions:
int inversions(List<Integer> numbers) {
int inversions = 0;
for (int i = 0; i < numbers.size(); i++) {
int n = numbers.get(i);
if (n <= 1) {
continue;
}
for (int j = i + 1; j < numbers.size(); j++) {
int m = numbers.get(j);
if (m > 0 && n > m) {
inversions++;
}
}
}
return inversions;
}
Finally, check the solvability:
boolean isSolvable(List<Integer> start, List<Integer> goal) {
int inversions = inversions(start);
int goalInversions = inversions(goal);
if (width % 2 == 0) {
int goalZeroRowIndex = goal.indexOf(0) / width;
int startZeroRowIndex = start.indexOf(0) / width;
// a little optimization is possible since we're only interested in parity
return (goalInversions % 2) == ((inversions + goalZeroRowIndex + startZeroRowIndex) % 2);
} else {
// for odd width just compare parity of inversions
return (inversions % 2) == (goalInversions % 2);
}
}
This function will work on both standard and random variations, regardless of 0 position.
For a given start and goal positions:
I(G) = 32
I(S) = 36
B(G) = 0
B(S) = 2
abs(B(G) - B(S)) = 2
I(G) is even and both I(S) and abs(B(G) - B(S)) have the same parity, so the puzzle is solvable. In fact, it is solvable in 31 moves:
12, 2, 11, 15, 2, 12, 9, 10, 6, 1, 4, 11, 15, 2, 12, 5, 14, 12, 5, 15, 2, 8, 11, 4, 3, 6, 10, 9, 6, 3, 1

Why am i getting nosuchelementexception in my code

Why am i getting NoSuchElementException?
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class GFG {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
for (int k = 0; k < t; k++) {
int n = sc.nextInt();
int xn = sc.nextInt();
int yn = sc.nextInt();
sc.nextLine();
ArrayList<Integer> l1 = new ArrayList<Integer>(2000);
ArrayList<Integer> l2 = new ArrayList<Integer>(2000);
int a[] = new int[2000];
int b[] = new int[2000];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
l1.add(a[i]);
}
sc.nextLine();
for (int i = 0; i < n; i++) {
b[i] = sc.nextInt();
l2.add(b[i]);
}
int tip = 0;
while ((xn > 0) && (yn > 0)&& (!l2.isEmpty())&& (!l1.isEmpty())) {
int pi = l1.indexOf(Collections.max(l1));
if (l1.get(pi) >= l2.get(pi)) {
tip += l1.get(pi);
xn--;
} else {
tip += l2.get(pi);
yn--;
}
l2.remove(pi);
l1.remove(pi);
}
if (yn > 0) {
while ((yn > 0) && (!l2.isEmpty())) {
tip += Collections.max(l2);
l2.remove(l2.indexOf(Collections.max(l2)));
yn--;
}
} else {
while ((xn > 0) && (!l1.isEmpty())) {
tip += Collections.max(l1);
l1.remove(l1.indexOf(Collections.max(l1)));
xn--;
}
}
System.out.println(tip);
}
}
}
Output:
Runtime Error: Runtime ErrorException in thread "main"
java.util.NoSuchElementException at
java.util.Scanner.throwFor(Scanner.java:862) at
java.util.Scanner.next(Scanner.java:1485) at
java.util.Scanner.nextInt(Scanner.java:2117) at
java.util.Scanner.nextInt(Scanner.java:2076) at
GFG.main(File.java:22)
I added some logs here:
int tip = 0;
while ((xn > 0) && (yn > 0)) {
System.out.println("l1:"+l1);
System.out.println("Collections.max(l1):"+Collections.max(l1));
int pi = l1.indexOf(Collections.max(l1));
Entring 1 15 9 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
I get a different error.
It prints out:
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Collections.max(l1):15
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Collections.max(l1):14
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Collections.max(l1):13
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Collections.max(l1):12
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Collections.max(l1):11
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Collections.max(l1):10
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9]
Collections.max(l1):9
l1:[1, 2, 3, 4, 5, 6, 7, 8]
Collections.max(l1):8
l1:[1, 2, 3, 4, 5, 6, 7]
Collections.max(l1):7
l1:[1, 2, 3, 4, 5, 6]
Collections.max(l1):6
l1:[1, 2, 3, 4, 5]
Collections.max(l1):5
l1:[1, 2, 3, 4]
Collections.max(l1):4
l1:[1, 2, 3]
Collections.max(l1):3
l1:[1, 2]
Collections.max(l1):2
l1:[1]
Collections.max(l1):1
l1:[]
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(ArrayList.java:854)
at java.util.Collections.max(Collections.java:669)
at GFG.main(GFG.java:31)
The problem is that you are calling Collections.max on en empty list.

What will happen if the conditon in the for loop is continuously changed?

What will be the result of the following block of code?
ArrayList<Integer> myList = new ArrayList<Integer>();
for (int i = 0; i < myList.size(); i++) {
myList.remove(i);
}
The answer is provided as the loop will never run.
But for an input of [-1, 3, 28, 17, 9, 33],
I got [3, 17, 33] as output. What does it mean?
The condition is evaluated on each iteration.
At the beginning the list contains
[-1, 3, 28, 17, 9, 33]
and the loop begins:
i = 0; 0 < 6 is true, current list [-1, 3, 28, 17, 9, 33], element with index 0 (bolded) removed, remaining [3, 28, 17, 9, 33]
i = 1; 1 < 5 is true, current list [3, 28, 17, 9, 33], element with index 1 (bolded) removed, remaining [3, 17, 9, 33]
i = 2; 2 < 4 is true, current list [3, 17, 9, 33], element with index 2 (bolded) removed, remaining [3, 17, 33]
i = 3; 3 < 3 is false, loop ends, remains [3, 17, 33]
Wrong! The loop will run as long as the conditions are still satisfied. Every iteration will produce a new scenario and the conditions just bend around to suit the new values.
Using your example [-1, 3, 28, 17, 9, 33]:
First iteration: i is 0... 0 is less than 6.. element at index 0 (-1) is hence removed
Resultant arrraylist: [3, 28, 17, 9, 33]
Second iteration: i is 1... 1 is less than 5.. element at index 1 (28) is hence removed
Resultant arrraylist: [3, 17, 9, 33]
Third iteration: i is 2... 2 is less than 4.. element at index 2 (9) is hence removed
Resultant arrraylist: [3, 17, 33]
Fourth iteration: i is 3... 3 is not less than 3.. Iteration stops.
After the loop operation, your MyList arraylist will be reduced to:
[3, 17, 33]
I hope this helps, merry coding!

Project Euler, problem 2- Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm a relatively new java programmer and I've been tinkering around with this program for the better part of the day now and I'm still stuck; I was hoping that you could help me with this.
So the program is supposed to meet the following requirements:
Each new term in the Fibonacci
sequence is generated by adding the
previous two terms. By starting with 1
and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the
Fibonacci sequence whose values do not
exceed four million, find the sum of
the even-valued terms.
This is my code:
//Generates Fibonacci sequence
while (fibNum < 144)
{
int lastValue = (Integer) fibList.get(fibList.size()-1);
int secondToLastValue = (Integer) fibList.get(fibList.size()-2);
fibNum = secondToLastValue + lastValue;
if (fibNum < 144)
{
fibList.add(fibNum);
}
//Picks out the even numbers from limitFibList
for (int i = 0; i < fibList.size(); i++)
{
if ((Integer) fibList.get(i) % 2 == 0)
{
evenNumsFibList.add(fibList.get(i));
}
}
//Sums up the total value of the numbers in the evenNumsFibList
for (int i = 0; i < evenNumsFibList.size(); i++)
{
sum += (Integer) evenNumsFibList.get(i);
}
...and this is the output that I'm getting:
Fibonacci sequence list: [1, 2, 3]
Size of the Fibonacci list: 3
Even Numbers list: [2]
Total sum of even numbers: 2
Fibonacci sequence list: [1, 2, 3, 5]
Size of the Fibonacci list: 4
Even Numbers list: [2, 2]
Total sum of even numbers: 6
Fibonacci sequence list: [1, 2, 3, 5, 8]
Size of the Fibonacci list: 5
Even Numbers list: [2, 2, 2, 8]
Total sum of even numbers: 20
Fibonacci sequence list: [1, 2, 3, 5, 8, 13]
Size of the Fibonacci list: 6
Even Numbers list: [2, 2, 2, 8, 2, 8]
Total sum of even numbers: 44
Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21]
Size of the Fibonacci list: 7
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8]
Total sum of even numbers: 78
Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34]
Size of the Fibonacci list: 8
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34]
Total sum of even numbers: 156
Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55]
Size of the Fibonacci list: 9
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 278
Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Size of the Fibonacci list: 10
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 444
Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Size of the Fibonacci list: 10
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 654
Obviously my while loop is contributing to my problems, but I don't know how to fix it.
Would greatly appreciate your help,
Haque
If you take a closer look at the numbers in the Fibonacci sequence that you actually need (only the even ones need to be summed), you will see a pattern:
0 1 1 2 3 5 8 13 21 34 55 89 144 ...
- O O E O O E O O E O O E
Notice that every 3rd number starting after 0 is even. Therefore, you can eliminate any checking for evenness if you calculate every third Fibonacci number. Looking again at the sequence, you can see that if k is the present even Fibonacci number you are looking at, and j is the one previous, the next even Fibonacci number n can be obtained by:
n = 4k + j
So in Java, you could try something like this:
int j = 0;
int k = 2;
int sum = j+k;
while (k < LIMIT) {
int tmp = 4*k + j;
sum = sum + tmp;
j = k;
k = tmp;
}
Looks like you are missing the close brackets on the while loop. So the other for's are running within it.
So:
while (fibNum < 144)
{
int lastValue = (Integer) fibList.get(fibList.size()-1);
int secondToLastValue = (Integer) fibList.get(fibList.size()-2);
fibNum = secondToLastValue + lastValue;
if (fibNum < 144)
{
fibList.add(fibNum);
}
}
//Picks out the even numbers from limitFibList
for (int i = 0; i < fibList.size(); i++)
{...
Am I missing something? Why do you need to create list? You just need a sum of even-valued numbers? Right? If I understand you correctly you can get your sum in 10 lines of code... I don't have Java IDE opend, so I'll give you Pythone code. If it is what you need I'll convert it into Java.
def fib(n=4000001): # write Fibonacci series up to n
r = 0
a, b = 0, 1
while b < n:
if not b%2 :
print(b, end=' ')
r += b
a, b = b, a+b
return r
OUTPUT:
2 8 34 144 610 2584 10946 46368 196418 832040 3524578
sum = 4613732
public class Euler002 {
int counter = 0;
public int getCounter () {
return counter;
}
public int getFibTotal () {
final int UPPER_LIMIT = 4000000;
int fib1 = 0;
int fib2 = 1;
int newFib = fib1 + fib2;
int total = 0;
while (newFib < UPPER_LIMIT) {
counter++;
fib1 = fib2;
fib2 = newFib;
newFib = fib1 + fib2;
if ((newFib % 2) == 0) {
total += newFib;
}
}
return total;
}
/**
* #param args
*/
public static void main(String[] args) {
Euler002 euler002 = new Euler002();
int total = euler002.getFibTotal();
System.out.println(" Counter = " + euler002.getCounter() + " And Fib Total is " + total);
}
}
The problem is here:
for (int i = 0; i < fibList.size(); i++)
{
if ((Integer) fibList.get(i) % 2 == 0)
{
evenNumsFibList.add(fibList.get(i)); <-- HERE
}
}
You're adppending a whole new list of all the even numbers to the end of the list you already have.
You need to delete everything in evenNumsFibList before calling this loop again, or modify the loop to only add even numbers that are not already in the list.
That's assuming your indentation is incorrect. If your indentation is actually how you want it, then you're simply missing a closing bracket on your while loop.

Categories

Resources