Skip to content

Instantly share code, notes, and snippets.

@AnimeshShaw
Created June 15, 2015 15:54
Bag DataStructure Test File
package com.codehackersblog.test;
import java.util.Iterator;
import com.codehackersblog.Bag.Bag;
/**
*
* @author psychocoder
*/
public class BagTest {
public static void main(String[] args) {
int initSize = 5;
/**
* Create an instance of a class Bag and initialize the constructor with
* an initial value.
*/
Bag<Integer> bag = new Bag<>(initSize);
System.out.println("Initial Size of Bag (No of elements): "
+ bag.getSize());
/**
* Get the capacity of the Bag
*/
System.out.println("Capacity (No. of items the bag can hold) :"
+ bag.capacity());
/**
* Insert some values.
*/
bag.add(12);
bag.add(212);
bag.add(1);
bag.add(123);
/**
* Get the Size
*/
System.out.println("Size of Bag after inserting items : "
+ bag.getSize());
/**
* Extend the size of the Bag
*/
bag.extendSize(initSize * 2);
/**
* Get the capacity of the Bag after extending the size
*/
System.out.println("Capacity of Bag after extending the size :"
+ bag.capacity());
/**
* Insert some more values.
*/
bag.add(142);
bag.add(12);
bag.add(19);
bag.add(131);
/**
* Get the Size
*/
System.out.println("Size of Bag after inserting more items : "
+ bag.getSize());
System.out.println("Elements present in the Bag :- \n");
int count = 0;
for (Iterator<Integer> itr = bag.iterator(); itr.hasNext(); count += 1) {
System.out.println(itr.next());
}
System.out.println("No. of elements in the Bag (When used counter) :"
+ count);
/**
* Clear all the elements from the bag
*/
bag.clearAll();
/**
* Get the Size of the bag after clearing all the items.
*/
System.out.println("Size of Bag after clearing : " + bag.getSize());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment