Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Index of List (http://www.velocityreviews.com/forums/t956935-index-of-list.html)

Subhabrata 01-25-2013 05:07 PM

Index of List
 
Dear Group,

I am a new programmer in Java.

I want to have check whether one word from one list is there in
another list and if it is
there I like to extract its next word.

For example,
If I have a string like,

"Moscow is the capital of Russia"

The wordlist would be "Moscow", "is","the","capital","of","Russia"

Now suppose I have a string of words like,

"Moscow", "Leningrad", "is", "was", "the", "a", "capital", "state",
"of", "by", "Russia", "Romania"

Here as one word is identified from the first string in the second
string the cursor points to the next word, rather it exploits hashing.

I know:
(i) Declaring String.
(ii)Using String.split
(iii) I can use the for loop over the string and find out indexing.
(iv) I know .equals

But I think I am missing something like if Java has .find or .index
sort of thing then it would be very easy to do.

Does Java have anything?

Regards,
Subhabrata.









Tim Slattery 01-25-2013 05:38 PM

Re: Index of List
 
Subhabrata <subhabangalore@gmail.com> wrote:


>But I think I am missing something like if Java has .find or .index
>sort of thing then it would be very easy to do.


There is an indexOf method in the String object.

--
Tim Slattery
Slattery_T@bls.gov

John B. Matthews 01-25-2013 05:48 PM

Re: Index of List
 
In article
<8fb20d2d-260b-42a1-9899-d038d2abfa5e@gg5g2000pbc.googlegroups.co
m>,
Subhabrata <subhabangalore@gmail.com> wrote:

>I am a new programmer in Java.
>
> I want to have check whether one word from one list is there
> in another list and if it is there I like to extract its next
> word.
>
> For example, If I have a string like,
>
> "Moscow is the capital of Russia"
>
> The wordlist would be "Moscow", "is","the","capital","of","Russia"
>
> Now suppose I have a string of words like,
>
> "Moscow", "Leningrad", "is", "was", "the", "a", "capital",
> "state", "of", "by", "Russia", "Romania"
>
> Here as one word is identified from the first string in the
> second string the cursor points to the next word, rather it
> exploits hashing.
>
> I know:
> (i) Declaring String.
> (ii)Using String.split
> (iii) I can use the for loop over the string and find out indexing.
> (iv) I know .equals
>
> But I think I am missing something like if Java has .find or .index
> sort of thing then it would be very easy to do.
>
> Does Java have anything?


Implementations of the java.util.List interface have a
convenient contains() method, Collections.binarySearch() may be
warranted for searching sufficiently long lists.

Implementations of the java.util.Map interface map keys to
values, often using hashing.

As you are new to Java, it might be worth starting here:

<http://docs.oracle.com/javase/tutorial/collections/index.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Arne Vajh°j 01-26-2013 02:44 AM

Re: Index of List
 
On 1/25/2013 12:07 PM, Subhabrata wrote:
> I want to have check whether one word from one list is there in
> another list and if it is
> there I like to extract its next word.
>
> For example,
> If I have a string like,
>
> "Moscow is the capital of Russia"
>
> The wordlist would be "Moscow", "is","the","capital","of","Russia"
>
> Now suppose I have a string of words like,
>
> "Moscow", "Leningrad", "is", "was", "the", "a", "capital", "state",
> "of", "by", "Russia", "Romania"
>
> Here as one word is identified from the first string in the second
> string the cursor points to the next word, rather it exploits hashing.
>
> I know:
> (i) Declaring String.
> (ii)Using String.split
> (iii) I can use the for loop over the string and find out indexing.
> (iv) I know .equals
>
> But I think I am missing something like if Java has .find or .index
> sort of thing then it would be very easy to do.
>
> Does Java have anything?


For inspiration:

import java.util.ArrayList;
import java.util.List;

public class Bad {
private static List<String> wordlist = new ArrayList<String>();
static {
wordlist.add("Moscow");
wordlist.add("Leningrad");
wordlist.add("is");
wordlist.add("was");
wordlist.add("the");
wordlist.add("a");
wordlist.add("capital");
wordlist.add("state");
wordlist.add("of");
wordlist.add("by");
wordlist.add("Russia");
wordlist.add("Romania");
}
public static void test(String s) {
for(String w : s.split("\\W")) {
int ix = wordlist.indexOf(w);
if(ix >= 0) {
System.out.println(w + " -> " + wordlist.get(ix + 1));
} else {
System.out.println(w);
}
}
}
public static void main(String[] args) {
test("Moscow is the capital of Russia X");
}
}

and

import java.util.HashMap;
import java.util.Map;

public class Good {
private static Map<String, String> translation = new HashMap<String,
String>();
static {
translation.put("Moscow", "Leningrad");
translation.put("is", "was");
translation.put("the", "a");
translation.put("capital", "state");
translation.put("of", "by");
translation.put("Russia", "Romania");
}
public static void test(String s) {
for(String w : s.split("\\W")) {
String w2 = translation.get(w);
if(w2 != null) {
System.out.println(w + " -> " + w2);
} else {
System.out.println(w);
}
}
}
public static void main(String[] args) {
test("Moscow is the capital of Russia X");
}
}

Arne



Roedy Green 01-27-2013 03:22 AM

Re: Index of List
 
On Fri, 25 Jan 2013 09:07:33 -0800 (PST), Subhabrata
<subhabangalore@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>
>I am a new programmer in Java.


HashSet will quickly tell you if a word is in a list, but it won't
give you any order.

You can export the data to a array or ArrayList and sort it. Then you
can linearly scan for it or use binary search.

Or you can build a HashMap that gives you the order# given the word.

This all blows up in you face if you keep adding words,

see
http://mindprod.com/jgloss/hashset.html
http://mindprod.com/jgloss/hashmap.html
http://mindprod.com/jgloss/arraylist.html
http://mindprod.com/jgloss/array.html
http://mindprod.com/jgloss/binarysearch.html
http://mindprod.com/jgloss/sort.html

Your question may be ill-formed. It seems to me there can be multiple
answers to the question.
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law


All times are GMT. The time now is 01:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.