1

I cant seem to figure out a way to use an ArrayList (2 or more indexes) to search within another ArrayList. For example, if list = [4,5,6,2,3,5,6] and searchlist = [5,6], index should = 1.

Anybody have any ideas?

7
  • so are you looking for the occurrence of the numbers in the searchlist to be in your list in the order that it is in in searchlist? and it return the index? Commented Mar 21, 2014 at 4:39
  • you want to search the second list within the first list.. right..?? Commented Mar 21, 2014 at 4:44
  • @mig Yea so if searchlist = [3,5], then index should = 4 Commented Mar 21, 2014 at 4:50
  • 1
    See a similar post at stackoverflow.com/questions/3940194/… Commented Mar 21, 2014 at 4:51
  • @Dileep Yea I want to search searchlist inside list Commented Mar 21, 2014 at 4:51

4 Answers 4

1

List has subList and equals. That's all you need.

import java.util.*;
import java.lang.*;
import java.io.*;


class TEST
{
public static void main (String[] args) throws java.lang.Exception  {
    List<Integer> list1 = Arrays.asList(1,2,3,4,5);
    List<Integer> list2 = Arrays.asList(2,3);

    for (int i=0; i<list1.size()-list2.size(); ++i) {
        if (list1.subList(i, i+list2.size()).equals(list2)) {
            System.out.println("found: "+i);
            return;
        }
    }

    System.out.println("not found");
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use pointers to your current indices

public int search(int[] list, int[] searchList) {
  int listIndex = 0;
  int searchListIndex = 0;
  int foundIndex = -1;

  while (listIndex != list.length) {
    if (list[listIndex] == searchList[searchListIndex]) {
      if (searchListIndex == 0) {
        foundIndex = listIndex;
      }
      listIndex++;
      searchListIndex++;
    }
    else {
      listIndex++;
      searchListIndex = 0;
      foundIndex = -1;
    }
  }
  return foundIndex;
}

Comments

1

Loop through the arrays in a nested loop. Then compare all values. If all values match up, then return the index:

list = [4,5,6,2,3,5,6]
searchlist = [5,6]

limit = list.size - serachList.size - 1

#make sure list's size is greater than or equal to searchList's size
for(i = 0 to limit)
    for(j = 0 to searchList.size-1)
        if(list[i + j] != searchList[j]) #found mismatch, so move on
            break
        if(j = searchList.size-1) #found complete match
            return i

Comments

0

The simplest idea is to run a loop, get the first value of the searchlist and find that value in the other list. If you find the number, increment the indices of both the lists and check the values again, continue until the end of searchlist or until the end of the other list.

I am not posting the code as I would rather you try yourself first, and come back if you are stuck anywhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.