0

Now this the question am trying to answer: Write a method which takes a sparse array as an argument and returns a new equivalent dense array.The dense array only needs to be large enough to fit all of the values.For example,the resulting dense array only needs to hold 90 values if the last element in the sparse array is at index 89.

dense array:[3,8,4,7,9,0,5,0] the number are generated randomly. sparse array is an arraylist of arrays [[0,3],[1,8],[2,4],[3,7],[4,9],[6,5]] so in the sparse array if the number generated is !0 the value and its index are stored in array of size 2 but if the number generated is 0 nothing is stored

3
  • What do you mean by dense? Flatten the arraylist? Commented Oct 15, 2015 at 0:53
  • @sam2090, no the sparse ArrayList is a list of array elements by their index positions and their values, where an element is omitted if it's value is 0. The dense array is a standard array where all elements have their values included, even if it is 0. Commented Oct 15, 2015 at 2:18
  • So, have you solved it? Commented Oct 16, 2015 at 17:21

4 Answers 4

2

When you have a fixed size for element (as array) in your collection. Your solution is OK and that is a fast way.

But when your element does not have a fixed size, such as: [[1,2,3],[4,5],[6],[7,8,9,10,11]] so you can interator through your element:

for(int[] e : sparseArr)
{
    for(int number : e)
    {
        tree.add(number);
    }
}

No matter how many element in your sparseArr, no how long of your element>

To sort your element, I recommend you should use TreeSet<E>, element push into tree will be sorted automatically.

Sign up to request clarification or add additional context in comments.

Comments

0

So if you just want to store 2 Integers paired together I recommend going with HashMaps. In your case you would use:

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

HashMaps support .containsKey(key); as well as .containsValue(value);

If you want to check all entries you can transform the Map to an entrySet:

for(Entry<Integer, Integer> e : map.entrySet()) {
    int one = e.getKey();
    int two = e.getValue();
}

Unless you want to do something more special than just storing 2 paired Integers I really can recommend doing it this way!

Comments

0

The method you're after should do something like this

public int[] sparseToDense (ArrayList<int[]> sparse) {
  int i = 0;
  int[] dense = new int[sparse.get(sparse.size()-1)[0]];
  int[] sp;
  ListIterator<int[]> iter = sparse.listIterator();
  while (iter.hasNext()) {
    sp = iter.next();
    while (sp[0] != i) {
      dense[i++] = 0;
    }
    dense[i++] = sp[1];
  }
  return dense;
}

3 Comments

i have an issue with sp = iter.Next(); i dont think i dont think it part of the listIterator methods
since sp isnt an object
next() should have a lowercase n have edited answer for this and set type of iterator objects to int[]
0

Just another way to do that, since you have java 8, you will be able to use stream. But if you're a beginner, i recommend you to try with for loops and arrays, will be better for your learning.

     public static ArrayList<Integer> returnDense(ArrayList<int[]> sparse) {
        return sparse.stream().flatMap(p -> IntStream.of(p).boxed())
              .collect(Collectors.toCollection(ArrayList::new));
     } 

also if you decide change int[] to Integer[].

public ArrayList<Integer> returnDense(ArrayList<Integer[]> sparse) {
    return sparse.stream().flatMap(p -> Arrays.asList(p).stream()).filter(Objects::nonNull)
        .collect(Collectors.toCollection(ArrayList::new));
    }

.filter(Objects::nonNull) is to be sure that will not have nulls values, but if you know that will not have it, that isn't necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.