I dont have any clue how to approach the problem i am facing. We have to print out an array using function.Function as a parameter and using lambas to print out 1. the even and 2. the odd numbers.
final class Main
{
private Main() {}
public static void main(String[] argv)
{
// Fill the array with integer values and print out only
// values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
// Even
ds.print(..);
}
// Odd
ds.print(...);
}
// end of main class
import java.util.Arrays;
import java.util.function.Function;
class DataStructure
{
// Create an array
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
int getSize()
{
return SIZE;
}
int[] getInts()
{
return arrayOfInts;
}
DataStructure()
{
// fill the array with ascending integer values
for (int i = 0; i < SIZE; i++)
{
arrayOfInts[i] = i;
}
}
private DataStructureIterator getIterator(IteratorStrategy strategy)
{
return strategy.getIterator(this);
}
void print(IteratorStrategy strategy)
{
DataStructureIterator iterator = getIterator(strategy);
while (iterator.hasNext())
{
System.out.print(iterator.next() + " ");
}
}
void printEven()
{
// Print out values of even indices of the array
DataStructureIterator iterator =
getIterator(newEvenIteratorStrategy());
while (iterator.hasNext())
{
System.out.print(iterator.next() + " ");
}
System.out.println();
}
//this method is still missing - we have to use the given parameters
void print(java.util.function.Function <Integer,Boolean> iterator)
{
}
}
The expected output is for isEven 0 2 4 6 8 10 12 14 and for isOdd 1 3 5 7 9 11 13
The rest of the given class shouldn't be important atleast i think so. I am searching for solutions or just a hint for hours but I dont find anything useful. Your help is much appreciated and sorry for my english If you need anything let me know. Btw I am new to stackoverflow so I hope I did everything right