Adding same elements in an array.,

J

justineee

Hello,

For example, I have an array of strings and these is its elements:

String[] array = new String [5];

array[0] = "blah";
array[1] = "hello";
array[2] = "help please";
array [3] = "pleaseeee";

then in the last element I would input "blah", how would I make my
program not add it into the array because "blah" is already present in
the array??

THANKS IN ADVANCE.
 
K

Knute Johnson

justineee said:
Hello,

For example, I have an array of strings and these is its elements:

String[] array = new String [5];

array[0] = "blah";
array[1] = "hello";
array[2] = "help please";
array [3] = "pleaseeee";

then in the last element I would input "blah", how would I make my
program not add it into the array because "blah" is already present in
the array??

THANKS IN ADVANCE.

You would look at all the elements of the array and compare them with
the element you are adding. If it exists, don't add it. Your elements
are Strings, you compare them with the String.equals() method. To look
at the list you need some sort of loop, but the for loop is probably the
simplest in this case. If this isn't a programming exercise for school,
look at LinkedHashSet, it will do what you want with considerably less code.
 
L

Lew

 justineee said:
For example, I have an array of strings and these is its elements:
String[] array = new String [5];
array[0] = "blah";
array[1] = "hello";
array[2] = "help please";
array [3] = "pleaseeee";
then in the last element I would input "blah", how would I make my
program not add it into the array because "blah" is already present in
the array??
THANKS IN ADVANCE.

The only way with an array is to check all previously populated entries
for the value and simply not do so.

A better way, IMHO, is not to use an Array.  Try using ArrayList
instead, and before each add call try the contains method.

An even better way is to use a java.util.Set. It automatically
maintains only one reference to 'equals()' objects.
 
D

Daniel Pitts

Steve said:
justineee said:
Hello,

For example, I have an array of strings and these is its elements:

String[] array = new String [5];

array[0] = "blah";
array[1] = "hello";
array[2] = "help please";
array [3] = "pleaseeee";

then in the last element I would input "blah", how would I make my
program not add it into the array because "blah" is already present in
the array??

THANKS IN ADVANCE.

The only way with an array is to check all previously populated entries
for the value and simply not do so.

A better way, IMHO, is not to use an Array. Try using ArrayList
instead, and before each add call try the contains method.
Or use a Set<String> set = new HashSet<String>();
set.add("blah");
set.add("hello");
etc...

Set is designed to not allow duplicates, which is exactly what the OP needs.
 
A

Arne Vajhøj

Daniel said:
Steve said:
For example, I have an array of strings and these is its elements:

String[] array = new String [5];

array[0] = "blah";
array[1] = "hello";
array[2] = "help please";
array [3] = "pleaseeee";

then in the last element I would input "blah", how would I make my
program not add it into the array because "blah" is already present in
the array??

The only way with an array is to check all previously populated
entries for the value and simply not do so.

A better way, IMHO, is not to use an Array. Try using ArrayList
instead, and before each add call try the contains method.
Or use a Set<String> set = new HashSet<String>();
set.add("blah");
set.add("hello");
etc...

Set is designed to not allow duplicates, which is exactly what the OP
needs.

LinkedHashSet that Knut suggested even keeps order.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top