elegant way for this?

What is the most elegant way for checking if

a value exists in a String array or not?

private String[] unique_dates = new String[1000];

for(int row = 0;row < tblDataTable.getRowCount();row++) {

if(? ? ?) {

unique_dates[row] = (String)tblDataTable.getValueAt(row,3);

System.out.println("row: " + row + ", date: " + unique_dates[row]);

}

}

I want the two middle rows only to be processed when a new unique value is found in the column.

[518 byte] By [aulo] at [2007-9-26 2:46:55]
# 1
use a hashtable and put the date as a key
mchan0 at 2007-6-29 10:30:56 > top of Java-index,Archived Forums,Java Programming...
# 2

Hi there

String searchString = "hello";

if(unique_dates[row].equals(searchString))

{

unique_dates[row] = (String)tblDataTable.getValueAt(row,3);

System.out.println("row: " + row + ", date: " + unique_dates[row]);

}

or

String searchString = "hello";

if(unique_dates[row].equalsIgnoreCase(searchString))

{

unique_dates[row] = (String)tblDataTable.getValueAt(row,3);

System.out.println("row: " + row + ", date: " + unique_dates[row]);

}

If you don't whant to search any longer then after the

if statement has come to true you should use a do while loop instead.

A "do while" loop, loops at least once but never more then you need

A "for" loop, allways runs till the end

If you are searching for stuff in larger arrays you should consider the use of searching algoritms

There are several algoritms developed for different problems.

Markus

markusbengtsson2 at 2007-6-29 10:30:56 > top of Java-index,Archived Forums,Java Programming...
# 3

This is not actually a recursively slow but if I have 1000 rows in a table, this will lead to 1+2+3+...+999 comparisons! And this is not what we want or do we?

THIS IS SLOW:

private String[] unique_dates = new String[1000];

for(int row = 0;row < tblDataTable.getRowCount();row++) {

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

if(? ? ?) {

unique_dates[row] = (String)tblDataTable.getValueAt(row,3);

System.out.println("row: " + row + ", date: " + unique_dates[row]);

}

}

}

Are there any "Compare" or UNIX "grep" like methods for that?

aulo at 2007-6-29 10:30:56 > top of Java-index,Archived Forums,Java Programming...
# 4
If I use a hashtable, can I then use any faster methods for checking the "uniqueness" of the date?Tnx in advance!
aulo at 2007-6-29 10:30:56 > top of Java-index,Archived Forums,Java Programming...
# 5

private String[] str_array = new String[1000];

populateStringArray(str_array);

String searchingFor = "hello";

if (java.util.Arrays.binarySearch(str_array, searchingFor)) {

// do something

}

oxbow_lakes at 2007-6-29 10:30:56 > top of Java-index,Archived Forums,Java Programming...
# 6

maybe I am not understanding, but keys in hashtables are unique. do a get, if it returns null there is no such entry, otherwise use the value (ie. put( theDate, theDate ) ).

> If I use a hashtable, can I then use any faster

> methods for checking the "uniqueness" of the date?

>

> Tnx in advance!

mchan0 at 2007-6-29 10:30:56 > top of Java-index,Archived Forums,Java Programming...
# 7

Oops - forgot to mention the sort. This might well take longer, but is nice and elegant.

private String[] str_array = new String[1000];

populateStringArray(str_array);

java.util.Arrays.sort(str_array);

String searchingFor = "hello";

if (java.util.Arrays.binarySearch(str_array, searchingFor)) {

// do something

}

oxbow_lakes at 2007-6-29 10:30:56 > top of Java-index,Archived Forums,Java Programming...
# 8
Tnx both guys! Got it working! :-)
aulo at 2007-6-29 10:30:56 > top of Java-index,Archived Forums,Java Programming...