0

I was wondering what would be the best way to implement this.

Can't think of a good way to save what information that needs to be saved like the index and the number of values and finally the actual number that get's repeated

public class testing 
{

public static void main(String[] args) 
{
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Scanner in = new Scanner(System.in);
    Integer a =0;
    Integer value = 0;
    Integer num = 0;

    boolean loop = true;
    //getting the string information
    while(loop)

    {
        System.out.println("Enter a series of numbers, 0 to stop");
        Integer n = in.nextInt();
        if(n.equals(0))
        {
            break;
        }
        else
        { 
            numbers.add(n);         

        }



    }

    for (int i = 1; i < numbers.size(); i++)
    { 




    }

}



}
user3307265
  • 35
  • 2
  • 7

1 Answers1

1

You could use a 2d ArrayList, declared like this:

ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

and then declare the 2 ArrayLists to be added to it at the end of the process:

ArrayList<Integer> length = new ArrayList<Integer>();

ArrayList<Integer> value = new ArrayList<Integer>();

Then

1) iterate through the list checking if the element is the same as previous.

If it is, carry on until end or an element is discovered which differs, at which point store the number of the previous equal elements in the ArrayList called 'length' and the value of the element in the one called 'value'. Have an int (called index say) which stores the index of the element in length containing the length of the longest current subsequence (which will be the same as the index of the element containing the value of the element it's made up of (which has been stored in value)).

If it's not, move to the next element.

2) Repeat the process, updating index if necessary (i.e. if a longer subsequence is discovered).

To add length and value to result at the end, just do result.add(length); and result.add(value);

If you want to return one object that holds all the required info, you could wrap the int 'index' in an Integer and add it to the end of the ArrayList called 'length' or even put it in a new ArrayList and add that ArrayList to result.

Note that to retrieve an element at index i in the first ArrayList (in this case the one called 'length') after it has been stored in result, you would need to do

result.get(0).get(i);

EDIT:

So the for loop part I had in mind would be this:

boolean same = false;
int sequenceLength = 0;
Integer sequenceInteger = null; 

for (int i = 1; i < numbers.size(); i++)
        { 
            if(numbers.get(i).equals(numbers.get(i-1)))
                {
                      same = true;
                      sequenceLength++;
                }      
            else(if same == true)
                {
                      sequenceInteger = new Integer(sequenceLength);
                      //add sequenceInteger to length and numbers.get(i-1) to value 
                      same = false;
                      sequenceLength = 0;
                }
            // else do nothing since same is false, which means that the current
            // element is different from the previous and the previous is 
            // different the one before that, so there are no new values to store
        }
// end of list reached
(if same == true)
{
      sequenceInteger = new Integer(sequenceLength);
      //add sequenceInteger to length and numbers.get(i-1) to value 
      same = false;
}
George Tomlinson
  • 1,871
  • 3
  • 17
  • 32
  • I would keep the initial array for the numbers? And the 2d array is used for storing the other 2 arrays length and value? And to start the comparing I would have to do something like Integer a = numbers.get[i]; and then if a = numbers.get[i+1] then add it to the list? – user3307265 Feb 27 '14 at 19:53
  • Yes to questions 1 and 2 (except that they are ArrayLists, not arrays). No to part 3 because i) to test for equality with integers I think you need to do if(numbers.get(i).compareTo(numbers.get(i-1))==0), and ii) you're not adding a number if it's equal to the next one (as you had in your third question). You're not even adding it if it's equal to the previous one. You're adding it if you find a list of at least 2 equal subsequent elements when you reach the first element immediately after this list of equal subsequent elements that differs or you reach the end of list. – George Tomlinson Feb 27 '14 at 20:28
  • (i.e you're adding the value of the previous element to the ArrayList 'value' if the current element is different from the previous element but the previous element is the same as the one before that). To do this, you could have a while loop within a while loop. The outer while loop terminates when the end of the list is reached. In the outer while loop, you have if (element i differs from element i-1) move to next element, else { while(boolean same == true) if (element i is equal to element i-1) increment an int storing length of sequence } else {same = false}. – George Tomlinson Feb 27 '14 at 20:28
  • (i.e you're adding the value of the previous element to the ArrayList 'value' if the current element is different from the previous element but the previous element is the same as the one before that). To do this, please see the EDIT to my answer. It's a better method than the one I suggested in the comments I've just deleted. – George Tomlinson Feb 27 '14 at 20:59
  • (i.e you're adding the value of the previous element to the ArrayList 'value' if the current element is different from the previous element but the previous element is the same as the one before that). To do this, please see the EDIT to my answer. It's a better method than the one I suggested in the comments I've just deleted. EDIT 2: for equality test I changed it to `if(numbers.get(i).equals(numbers.get(i-1)))`. compareTo is for when you're testing whether it's equal to, less than or greater than rather than equal or not equal, as in this case. – George Tomlinson Feb 27 '14 at 21:40
  • EDIT 3: forgot to set sequenceLength to 0 when end of a sequence reached (just amended answer accordingly). – George Tomlinson Feb 28 '14 at 10:29