I am the following when i try to run my java program. I have included the code below
CollectionExamp le1.java java.util.Colle ction is abstract; cannot be instatiated
Collection col=new Collection();
CODE
CollectionExamp le1.java java.util.Colle ction is abstract; cannot be instatiated
Collection col=new Collection();
CODE
Code:
import java.util.*;
class CollectionsExample1
{
static void CollectionMethod()
{
Collection col=new Collection();
col.add("J");
col.add("e");
col.add("s");
col.add("u");
col.add("s");
boolean cc=col.contains("g");
System.out.println("\n");
System.out.println(cc);
System.out.println("\n");
col.remove("u");
System.out.println("u has been removed");
System.out.println("\n");
Iterator gh=col.iterator();
while(gh.hasNext())
{
Object element=gh.next();
System.out.println(element);
}
}
static void SetsMethod()
{
// Create the set
Set set = new HashSet();
// Add elements to the set
set.add("L");
set.add("O");
set.add("R");
set.add("D");
set.add("D");
// Get number of elements in set
int size = set.size();
System.out.println("The size of the Set is "+size);
//Adding an element that already exists in the set has no effect
System.out.println("Adding O has no effect on size of the set");
set.add("O");
size = set.size();
System.out.println("The size of the Set is "+size);
// Determining if an element is in the set
boolean b = set.contains("L");
System.out.println(b);
b = set.contains("c");
System.out.println(b);
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext())
{
// Get element
Object element = it.next();
System.out.println(element);
}
// Remove elements from the set
set.remove("D");
// Create an array containing the elements in the set (in this case a String array)
String[] array = (String[])set.toArray(new String[set.size()]);
for(int i=0;i<set.size();i++)
System.out.println(array[i]);
}
static void ArrayListMethod()
{
// Create the sorted set
SortedSet set = new TreeSet();
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
set.add("z");
set.add("a");
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext())
{
// Get element
Object element = it.next();
System.out.println(element);
}
// The elements are iterated in order: a, b, c
// Create an array containing the elements in a set (in this case a String array).
// The elements in the array are in order.
String[] array = (String[])set.toArray(new String[set.size()]);
System.out.println("\n\n");
for(int i=0;i<set.size();i++)
System.out.println(array[i]);
}
public static void main(String [] args)
{
SetsMethod();
ArrayListMethod();
CollectionMethod();
}
}
Comment