A Bag is a collection where removing items is not supported. It purpose
is to provide users with the ability to collect items and then to
iterate through the collected items. At the end you have an option to
empty the complete bag.
In this implementation you have an additional method to increase the Bag storage.
In this implementation you have an additional method to increase the Bag storage.
You can get the eclipse project and the project jar from here.
You can simply run the jar file from the terminal as :-
java -jar Bag.jar
It will simply show you the output the sample test file for the Bag DS. I will provide the source for the test file which shows how to use it. Here's the code :-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Output :-
Initial Size of Bag (No of elements): 0 Capacity (No. of items the bag can hold) :5 Size of Bag after inserting items : 4 Capacity of Bag after extending the size :10 Size of Bag after inserting more items : 8 Elements present in the Bag :- 12 212 1 123 142 12 19 131 No. of elements in the Bag (When used counter) :8 Size of Bag after clearing : 0
I hope you liked this post. Share it in your social networks.
Thank you.