Finish the code wherever it says TODO
/**
* Node type for this list. Each node holds a maximum
of nodeSize elements in an
* array. Empty slots are null.
*/
private class Node {
/**
* Array of actual data
elements.
*/
// Unchecked warning
unavoidable.
public E[] data = (E[]) new
Comparable[nodeSize];
/**
* Link to next node.
*/
public Node next;
/**
* Link to previous node;
*/
public Node previous;
/**
* Index of the next available
offset in this node, also equal to the number of
* elements in this node.
*/
public int count;
/**
* Adds an item to this node at the
first available offset. Precondition: count
* < nodeSize
*
* @param item element to be
added
*/
void addItem(E item) {
if (count >=
nodeSize) {
return;
}
data[count++] =
item;
// useful for
debugging
//
System.out.println("Added " + item.toString() + " at index " +
count + " to
// node " +
Arrays.toString(data));
}
/**
* Adds an item to this node at the
indicated offset, shifting elements to the
* right as necessary.
*
* Precondition: count <
nodeSize
*
* @param offset array index at
which to put the new element
* @param item element to be
added
*/
void addItem(int offset, E item)
{
if (count >=
nodeSize) {
return;
}
for (int i =
count - 1; i >= offset; --i) {
data[i + 1] = data[i];
}
++count;
data[offset] =
item;
// useful for
debugging
// System.out.println("Added " + item.toString() + " at index " +
offset + " to node: " + Arrays.toString(data));
}
/**
* Deletes an element from this node
at the indicated offset, shifting elements
* left as necessary. Precondition:
0 <= offset < count
*
* @param offset
*/
void removeItem(int offset) {
E item =
data[offset];
for (int i =
offset + 1; i < nodeSize; ++i) {
data[i - 1] = data[i];
}
data[count - 1]
= null;
--count;
}
}
private class StoutListIterator implements
ListIterator<E> {
// constants you possibly use
...
// instance variables ...
/**
* Default constructor
*/
public StoutListIterator() {
// TODO
}
/**
* Constructor finds node at a given
position.
*
* @param pos
*/
public StoutListIterator(int pos)
{
// TODO
}
@Override
public boolean hasNext() {
// TODO
}
@Override
public E next() {
// TODO
}
@Override
public void remove() {
// TODO
}
// Other methods you may want to
add or override that could possibly facilitate
// other operations, for instance,
addition, access to the previous element,
// etc.
//
// ...
//
}
/**
* Sort an array arr[] using the insertion sort
algorithm in the NON-DECREASING
* order.
*
* @param arr array storing elements from the
list
* @param comp comparator used in sorting
*/
private void insertionSort(E[] arr, Comparator<?
super E> comp) {
// TODO
}
/**
* Sort arr[] using the bubble sort algorithm in the
NON-INCREASING order. For a
* description of bubble sort please refer to Section
6.1 in the project
* description. You must use the compareTo() method
from an implementation of
* the Comparable interface by the class E or ? super
E.
*
* @param arr array holding elements from the
list
*/
private void bubbleSort(E[] arr) {
// TODO
}
}
1
So I need to remove empty (undefined) items from the
multidimensional array. Atm my code looks like this (it's a method
I run so that's why i am using this
:
f: function(arr) {
var __ = this;
arr = arr.filter(function(item) {
return Array.isArray(item) ? __.f(item) : typeof(item) !== "undefined";
});
return arr;
}
but if i run console.log(myObject.f([1, 2, , , , , 3, 4,
[5, , , , , ], 6, , , , 8, 3, [[[], 9]]]));
i get [ 1,
2, 3, 4, [ 5, , , , ], 6, 8, 3, [ [ [], 9 ] ] ]
and that is
kinda weird result. I goes pretty well for the first layer but
removes only one undefined from inner layers. Also I would like to
remove a subarray that consists of no items.
Get Answers For Free
Most questions answered within 1 hours.