adding characters to a collection

Hello, I've been working on this project where I have to add characters from and input string into a bag. I have the program and the bag collection working properly. I'm trying to adjust the add() method so it will only insert a character into the collection if no duplicate values exist, and return true. If there is a duplicate value, it should return false.

Here is my snippit of code for adding characters to "NodupBagA":

// add characters from the string to NodupBagA

for (int i = 0; i < str.length(); i++)

NodupBagA.add(str.charAt(i));

Here is my add() method in the NodupBag Class:

/**

* Inserts item in the NodupBag if space is available; that is,

* it the size is less than the capacity.

* Returns <tt>true</tt> if a new element is added and <tt>false</tt>

* if a duplicate value exists.

*

* @param item element that is added if space is available..

* @return <tt>true</tt> if a new element is inserted in the NodupBag.

*

*/

publicboolean add(T item)

{

boolean returnValue;

if (NodupBagSize >= NodupBagArr.length)

returnfalse;

else

{

// append item at index bagSize as long as no duplicates exist

NodupBagArr[NodupBagSize] = item;

// increment bagSize and return true

NodupBagSize++;

returntrue;

}

}

How would I get this to return false if a duplicate value is entered?

[2141 byte] By [djsperlinga] at [2007-11-26 21:55:27]
# 1

Loop over the existing characters and compare them to the inserted character.

If there's a match,

return false

If you end the loop with no match,

add it

return true.

hunter9000a at 2007-7-10 3:51:18 > top of Java-index,Java Essentials,New To Java...
# 2

> Here is my snippit of code for adding characters to

> "NodupBagA":

>

> // add characters from the string to NodupBagA

> for (int i = 0; i < str.length(); i++)

> NodupBagA.add(str.charAt(i));

THIS IS EXACTLY WHY I HATE AUTOBOXING

NodupBagA.add(str.charAt(i));

str.charAt() returns a char which is NOT an Object.

What Java version are you using?

public boolean add(T item)

What is T that it is accepting chars?

TuringPesta at 2007-7-10 3:51:18 > top of Java-index,Java Essentials,New To Java...
# 3

Funny. This is an example of why I love autoboxing ;-)

import java.util.*;

public class SetExample {

public static void main(String[] args) {

Set < Character > chars = new HashSet < Character > ();

System.out.println(addAll(chars, "abc")); //true

System.out.println(addAll(chars, "defd")); //false

System.out.println(addAll(chars, "ghi")); //true

System.out.println(addAll(chars, "jkb")); //false

}

//returns true iff s.length characters were added to chars -- no dups

static boolean addAll(Set < Character > chars, String s) {

int oldSize = chars.size();

for(char ch : s.toCharArray())

chars.add(ch);

return chars.size() == oldSize + s.length();

}

}

DrLaszloJamfa at 2007-7-10 3:51:18 > top of Java-index,Java Essentials,New To Java...
# 4
I actually just adjusted the Bag collection. The bag collections is a generic collection anyway. I'm just trying a simple way of implementing it. Theoretically, I could write the code to accept strings, int, and time24 objects.
djsperlinga at 2007-7-10 3:51:18 > top of Java-index,Java Essentials,New To Java...