Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Matching Items in a vector

Thread view: 
nemadrias - 01 Aug 2006 19:51 GMT
Hi -
I was using two arrays to go through two lists of numbers in the same
format, such as below:

The numbers are always 15 numbers followed by some text.  I ONLY care
about the first 15 numbers, not the text.

100121242110124ABC        000121241000124ABC
000121236300124ABC        000121236300124ABC
005151533414124ABC        001121231009909ABC
000124576700188ABC        123516800785656ABC

I had passed both sets of numbers into two separate arrays, and was
able to use the regionMatches method to compare the first 15 numbers to
find duplicates.  I want to be able to remove any duplicates from the
first array so I can return only the unique numbers from the first list
(left side).  Unfortunately, arrays don't have a "removeAtIndex" method
while Vectors DO have the ability to remove from the vector array.  So
I tried vectors...But, Vectors can't use the method regionMatches which
allows me to check those first 15 numbers...

Can ANYONE help me with this problem which has been plaguing me for
hours now?  Thanks in advance: I can definitely provide more info. if
needed.  THANKS!!
Steve

--Sample of code below with Vectors (which can't use the regionMatches
method)

for (i = 0; i < 3; i++) {
   for (j = 0; j < 3; j++) {
        if
(myVector.elementAt(i).regionMatches(0,myVector2.elementAt(j),0,15)){
    myVector.removeElementAt(i);
        }
        else {
        //Do nothing
   }
}
Oliver Wong - 01 Aug 2006 20:59 GMT
> Hi -
> I was using two arrays to go through two lists of numbers in the same
[quoted text clipped - 24 lines]
> --Sample of code below with Vectors (which can't use the regionMatches
> method)

   regionMatches() is a method on String, not on arrays.

   If you're using 1.5, declare your vectors as containg strings.
Otherwise, extract the value from the vector and cast it to string.

> for (i = 0; i < 3; i++) {
>    for (j = 0; j < 3; j++) {
[quoted text clipped - 6 lines]
>    }
> }

   You're gonna have problems if your input data was such that you needed
to remove two consecutive items from myVector2.

   - Oliver
Michael Rauscher - 01 Aug 2006 21:12 GMT
nemadrias schrieb:
> Hi -
> I was using two arrays to go through two lists of numbers in the same
[quoted text clipped - 16 lines]
> I tried vectors...But, Vectors can't use the method regionMatches which
> allows me to check those first 15 numbers...

I consider that there are at least two entries where the three letters
at the end differ.

If you put Strings into a Collection (Vector implements this interface),
you can use String#regionMatches on it's elements, of course.

If you use Java >= 1.5 you can use generics to make the Vector type-safe:

Vector<String> myVector = new Vector<String>();

Then you can use regionMatches without a cast:
  if ( myVector.get(i).regionMatches(...

If you don't use Java >= 1.5 you have to cast:

  if ( ((String)myVector.get(i)).regionMatches(...

BTW: if you don't access myVector in more than one thread, you should
use ArrayList instead of Vector since Vector is synchronized.

Second: you could have used a custom class to represent your entries.

class Entry {
    private String content;

    public Entry( String content ) {
        if ( content == null )
            throw new IllegalArgumentException(
                    "Cannot have null content.");

        if ( content.length() < 15 )
            throw new IllegalArgumentException(
                    "Content too short" );

        this.content = content;
    }

    public String toString() {
        return content;
    }

    public boolean equals( Object o ) {
        if ( o == null || !(o instanceof Entry) )
            return false;
        return (content.regionMatches(0, o.toString(), 0, 15));
    }

    public int hashCode() {
        return content.substring(0, 15).hashCode();
    }
}

With this class, you can manage your entries in the form of collections
(Java 1.5 syntax):

HashSet<Entry> firstSet = new HashSet<Entry>();
HashSet<Entry> secondSet = new HashSet<Entry>();

// fill the sets, e. g. at loading time or if you only have
// vectors:
// for ( String s : myVector )
//    firstSet.add( new Entry(s) );
// for ( String s : myVector2 )
//    secondSet.add( new Entry(s) );

// now, remove the duplicates:
firstSet.removeAll(secondSet);

Of course, this works with any Collection not just sets.

Third: ... there are many ways...

Bye
Michael
steve - 01 Aug 2006 22:04 GMT
> Hi -
> I was using two arrays to go through two lists of numbers in the same
[quoted text clipped - 35 lines]
>     }
> }

why don't you stick the number in a hashmap, then before you add the next
number, check if it is in the hashmap, if it is not add it.
And if it is already in the map , then do whatever you are going to do.

I think you will find it faster than double looping

steve


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.