Problem with task in java
hi i have this condition to solve :
//Write a method that finds the longest
//subsequence of equal numbers in given
//List and stores it in another List.
So far i did the next thing that by my opinion needs to work but it doesnt, any ideas?
Code:
import java.util.ArrayList;
public class Zadacha_4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Write a method that finds the longest
//subsequence of equal numbers in given
//List and stores it in another List.
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(2);
numbers.add(3);
numbers.add(3);
numbers.add(3);
numbers.add(1);
numbers.add(1);
ArrayList<Integer> longSec = new ArrayList<Integer>();
Integer[] arr = new Integer[]{};
arr = numbers.toArray(arr);
int bigcounter = 0;
int counter=0;
for (int i = 0; i < arr.length-1; i++ )
{
if (arr[i] == arr[i+1])
{
counter++;
longSec.add(arr[i]);
if (counter > bigcounter)
{
bigcounter=counter;
longSec.add(arr[i]);
}
else longSec.clear();
}
counter = 0;
}
}
}
|