Index of List

Discussion in 'Java' started by Subhabrata, Jan 25, 2013.

  1. Subhabrata

    Subhabrata Guest

    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.
    Subhabrata, Jan 25, 2013
    #1
    1. Advertising

  2. Subhabrata

    Tim Slattery Guest

    Subhabrata <> 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
    Tim Slattery, Jan 25, 2013
    #2
    1. Advertising

  3. In article
    <
    m>,
    Subhabrata <> 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>
    John B. Matthews, Jan 25, 2013
    #3
  4. Subhabrata

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Jan 26, 2013
    #4
  5. Subhabrata

    Roedy Green Guest

    On Fri, 25 Jan 2013 09:07:33 -0800 (PST), Subhabrata
    <> 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
    Roedy Green, Jan 27, 2013
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karthikeyavenkat
    Replies:
    2
    Views:
    553
    Bryce
    Mar 17, 2005
  2. Shawn W_
    Replies:
    5
    Views:
    256
    Aldric Giacomoni
    Sep 16, 2009
  3. ngoc
    Replies:
    5
    Views:
    165
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    255
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    171
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page