Java tutorial
//package com.java2s; /** * This file is part of the Joana IFC project. It is developed at the * Programming Paradigms Group of the Karlsruhe Institute of Technology. * * For further details on licensing please read the information at * http://joana.ipd.kit.edu or contact the authors. */ import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class Main { public static <A> boolean isSuffixOf(final List<A> ls1, final List<A> ls2) { if (ls1 == null) { return true; } if (ls2 == null) { return false; } if (ls1.size() > ls2.size()) { return false; } final List<A> ls1Rev = new LinkedList<A>(ls1); final List<A> ls2Rev = new LinkedList<A>(ls2); Collections.reverse(ls1Rev); Collections.reverse(ls2Rev); return isPrefixOf(ls1Rev, ls2Rev); } public static <A> boolean isPrefixOf(final List<A> ls1, final List<A> ls2) { if (ls1 == null) { return true; } if (ls2 == null) { return false; } if (ls1.size() > ls2.size()) { return false; } final Iterator<A> iter1 = ls1.iterator(); final Iterator<A> iter2 = ls2.iterator(); while (iter1.hasNext()) { final A x1 = iter1.next(); final A x2 = iter2.next(); if (!x1.equals(x2)) { return false; } } return true; } }