Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

collection sublist

hi,

I was running following example

import java.util.*;

public class d {
   public static void main(String[] args) {
      List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
      System.out.println("List :"+list);
      List sublist = Arrays.asList("three Four".split(" "));
      System.out.println("SubList :"+sublist);
      System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist));
      System.out.println("lastIndexOfSubList: "
      + Collections.lastIndexOfSubList(list, sublist));
   }
}


from link
http://www.tutorialspoint.com/javaexamples/collection_sublist.htm
Iam getting output as

List :[one-Two, three, Four, five, six, one, three, Four]
SubList :[three, Four]
indexOfSubList: 1
lastIndexOfSubList: 6



I have not understood meaning of sublist its index concept

>> List sublist = Arrays.asList("three Four".split(" "));

what above line do .

Why

indexOfSubList: 1
lastIndexOfSubList: 6

is getting printed.

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

A 'sublist' is just a list that's part of another list
Avatar of gudii9

ASKER

indexOfSubList: 1
lastIndexOfSubList: 6


what is 1 and 6 here . Please advise
Avatar of gudii9

ASKER

the link says it should get 2,7 like below..but i am getting 1, 6 not sure why
indexOfSubList: 2
lastIndexOfSubList: 7
Why

indexOfSubList: 1
lastIndexOfSubList: 6

is getting printed.
You must be running different code
Avatar of gudii9

ASKER

ia m running same code as below

import java.util.*;

public class d {
   public static void main(String[] args) {
      List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
      System.out.println("List :"+list);
      List sublist = Arrays.asList("three Four".split(" "));
      System.out.println("SubList :"+sublist);
      System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist));
      System.out.println("lastIndexOfSubList: "
      + Collections.lastIndexOfSubList(list, sublist));
   }
}



getting output as
List :[one, Two, three, Four, five, six, one, three, Four]
SubList :[three, Four]
indexOfSubList: 2
lastIndexOfSubList: 7


please advise
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial