Reference based linked list Node question.

Say I have a something like this;

char[] charArray = getCharArray();

int arraySize =100000;

Node[] person =new Node[arraySize];

and

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

person[i] =new Node(new Person(getNames(arraySize,charArray),randomAge(),randomIncome()));

}

What I have to do is search through the array "person" and delete any objects which have the same value for "name", the getName() method which is called in the for loop calls generates a random name.

I have to make sure duplicate names don't exisist in the array.

Im not really sure how to access and check the type of object in the Node array, the stored objects are of type "Person" and has data fields for name,age and income.

Someone please help!

Ta!

[1088 byte] By [SpartanNZa] at [2007-11-27 2:25:51]
# 1

I'm not totally sure what you mean, but if you want to delete double values, there is no faster way then to use a nested loop afaik.

something like this:

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

{

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

{

if( person[i].getName().equals(person[j].getName()))

{

//Duplicate !

}

}

}

It is not exactly what you asked for but it might help you in the right direction.

radicjesa at 2007-7-12 2:34:41 > top of Java-index,Java Essentials,Java Programming...
# 2

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

for (int j = i ; j < person.length ; j++)

if (person[j] != null)

if (person[i].getName().compareTo(person[j].getName()) == 0)

person[j] = null;

notice that it's the very basic way of doing it, but not a really good one since you will have null pointers in your array if there are dups in the list

the smart way when you want values with no dups is to use a Set

see http://java.sun.com/docs/books/tutorial/collections/interfaces/set.html

calvino_inda at 2007-7-12 2:34:41 > top of Java-index,Java Essentials,Java Programming...
# 3
But the thing is, if the names are the same, and the ages and incomes are different will this loop set the array element to null?Like say for example:Name: Sam Age:21 Income:20000Name:Sam Age:35 Income:100000I need to make sure if the NAMES are the not the
SpartanNZa at 2007-7-12 2:34:41 > top of Java-index,Java Essentials,Java Programming...
# 4

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

{

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

{

if( person[i].getName().equals(person[j].getName()))

{

StringBuffer sB = new StringBuffer();

sB.append( person[i].getName());

sB.append( j );

person[j].setName( sB.toString());

}

}

}

This way if it finds the same name, it will give the same name a different number behind it.

So if you have

Name: Sam Age: 21

Name: Sam Age: 35

Will become:

Name: Sam Age: 21

Name: Sam1 Age: 35

radicjesa at 2007-7-12 2:34:41 > top of Java-index,Java Essentials,Java Programming...