logo Practice-It logo


Related Links:
Author: Whitaker Brand (on 2014/02/14)

Write a method called fromCounts that converts an ArrayIntList of counts into a new ArrayIntList of values. Assume that the ArrayIntList that is called stores a sequence of integer pairs that each indicate a count and a number. For example, suppose that an ArrayIntList called list stores the following sequence of values:

        [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17]

This sequence of pairs indicates that you have 5 occurrences of 2, followed by two occurrences of -5, followed by 4 occurrences of 3, and so on. If we make the following call:

        ArrayIntList list2 = list.fromCounts();

Then the variable list2 should store the following sequence of values:

        [2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17]

You are writing a method for the ArrayIntList class discussed in lecture:

        public class ArrayIntList {
            private int[] elementData; // list of integers
            private int size;          // current # of elements in the list


You may assume that the ArrayIntList that is called stores a legal sequence of pairs (which means it will always have an even size) and that the default constructor for ArrayIntList will construct an array of sufficient capacity to store the result. Your method should not change the original list. If the sequence of pairs is empty, the result should be an empty list.

You may call the ArrayIntList constructor, but otherwise you may not call any other methods of the ArrayIntList class to solve this problem. You are not allowed to define any auxiliary data structures (no array, String, ArrayList, etc), and you may not change the original list.

Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.