1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem with task in java

Discussion in 'Java' started by checho, Dec 30, 2009.

  1. checho

    checho New Member

    Joined:
    Dec 18, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
    
    
    }
    
    }
     
    Last edited by a moderator: Dec 30, 2009

Share This Page