Refine search
LinkedList<Integer> inQueue = new LinkedList<Integer>(); LinkedList<Integer> outQueue = new LinkedList<Integer>(); final byte[] search, replacement; Iterator<Integer> inIter = inQueue.iterator(); for (int i = 0; i < search.length; i++) if (!inIter.hasNext() || search[i] != inIter.next()) while (inQueue.size() < search.length) { int next = super.read(); inQueue.offer(next);
@Override public E set(int location, E object) { ListIterator<E> it = listIterator(location); if (!it.hasNext()) { throw new IndexOutOfBoundsException(); } E result = it.next(); it.set(object); return result; } }
String a = "apple"; String listelement = "a bunch of apples"; LinkedList<String> list = new LinkedList<String>(); list.add(listelement); Iterator<String> li = list.iterator(); while (li.hasNext()) { if (li.next().contains(a)) { System.out.println("Hooray!"); } }
/** * Loads the MRU file list in from a file and stores it in a LinkedList. * If no file exists, a new LinkedList is created. */ protected void load() { createConfigurationDirectory(); File file = new File(getFilename()); if (file.exists()) { try { ObjectInputStream ois = new ObjectInputStream( new FileInputStream(file)); _mruFileList = (LinkedList) ois.readObject(); ois.close(); // check that only files and url are in linked list Iterator it = _mruFileList.iterator(); while (it.hasNext()) { Object o = it.next(); if (!(o instanceof File) && !(o instanceof URL)) { it.remove(); } } } catch (Exception e) { _mruFileList = new LinkedList(); } } else { _mruFileList = new LinkedList(); } }
String s = "1/2/3" char[] c = s.toCharArray(); LinkedList<String> ll = new LinkedList<String>(); int index = 0; for(int i=0;i<c.length;i++) { if(c[i] == '/') { ll.add(s.substring(index,i)); index = i+1; } } String[] arr = ll.size(); Iterator<String> iter = ll.iterator(); index = 0; for(index = 0; iter.hasNext(); index++) arr[index++] = iter.next();
list = new LinkedList<Point>(); list.clear(); for (Iterator<Point> it = list.iterator(); it.hasNext(); ) { list = new LinkedList<Point>();
LinkedList<Integer> list = new LinkedList<Integer>(); list.add(1); list.add(20); list.add(30); Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()) System.out.print(iterator.next() + " "); list.add(1,600); System.out.println(list); list.remove(1); System.out.println(list); System.out.println("now using linked list specific 2 methods..."); list.addFirst(901); list.addLast(902); Iterator<Integer> iterator1 = list.iterator(); while(iterator1.hasNext()) System.out.print(iterator1.next()+" "); //this line throws Exception:
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("This"); linkedList.add("is"); linkedList.add("an"); linkedList.add("test"); LinkedList<String> temp = new LinkedList<String>(); for (Iterator<String> iterator = linkedList.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); if(string.equals("an")) { iterator.remove(); temp.add("a"); } } linkedList.addAll(temp);
public static <T extends Comparable<? super T>> void deleteReps(LinkedList<T> list) { LinkedList<T> noRepsList = new LinkedList<T>(); Iterator<T> itr = list.iterator(); while(itr.hasNext()) { T currentTest = itr.next(); if (!noRepsList.contains(currentTest)) noRepsList.add(currentTest); else itr.remove(); } }
import java.util.Iterator; import java.util.LinkedList; public class BoundedStack<T> implements Iterable<T> { private final LinkedList<T> ll = new LinkedList<T>(); private final int bound; public BoundedStack(int bound) { this.bound = bound; } public synchronized void push(T item) { ll.push(item); if (ll.size() > bound) { ll.removeLast(); } } public synchronized T pop() { return ll.poll(); } public synchronized Iterator<T> iterator() { return ll.iterator(); } }
LinkedList<Integer> ll = new LinkedList<Integer>(); ll.add(1); ll.add(2); ll.add(3); System.out.println("Normal traversal"); for(Iterator<Integer> i = ll.iterator(); i.hasNext();) { System.out.println(i.next()); } System.out.println("Reverse"); for(Iterator<Integer> i = ll.descendingIterator(); i.hasNext();) { System.out.println(i.next()); }
public class HackedLinkedHashMap<K,V> extends LinkedHashMap<K,V> implements Map<K,V> { private LinkedList<K> keyList = new LinkedList<>(); @Override public V put(K key, V value){ V obj = super.put(key, value); keyList.addFirst(key); return obj; } public LinkedList<K> getReversedKeyList(){ return keyList; }} public static void main(String[] args) { System.out.println("Test conference"); HackedLinkedHashMap<Integer, Integer> map = new HackedLinkedHashMap(); for(Integer i=0;i<100;i++){ map.put(i, i); } Iterator<Integer> iterator = map.getReversedKeyList().iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } }
// LinkedLists have a member variable called modCount // which is an integer which holds the count of modifications // made on the list with methods like list.add() list.remove() etc. LinkedList ll =new LinkedList(); . . . . . . // When the iterator is created here, it copies the current modCount // of the LinkedList into a member of its own called expectedModCount Iterator iter = ll.iterator(); int i= 0; while (iter.hasNext()) { // iter.next() calls a method called checkForComodification // which throws a ConcurrentModificationException if the // current modCount of the original LinkedList is different // from the expectedModCount on this iterator String s1 = (String) iter.next(); ll.remove(i); i++; }
@Override public E remove(int location) { try { ListIterator<E> it = listIterator(location); E result = it.next(); it.remove(); return result; } catch (NoSuchElementException e) { throw new IndexOutOfBoundsException(); } }
@Override public E get(int location) { try { return listIterator(location).next(); } catch (NoSuchElementException e) { throw new IndexOutOfBoundsException(); } }
public class ReversedLinkedList<T> extends LinkedList<T> { private final LinkedList<T> list; public ReversedLinkedList(LinkedList<T> list) { this.list = list; } public Iterator<T> descendingIterator() { return list.iterator(); } public Iterator<T> iterator() { return list.descendingIterator(); } public T get(int index) { int actualIndex = list.size() - index - 1; list.get(actualIndex); } // Etc. }
protected void endStep() { Step step = (Step) peekFrame().removeFirst(); addPredicates( step, popFrame().iterator() ); push( step ); }
@Override public boolean addAll(int location, Collection<? extends E> collection) { ListIterator<E> it = listIterator(location); Iterator<? extends E> colIt = collection.iterator(); int next = it.nextIndex(); while (colIt.hasNext()) { it.add(colIt.next()); } return next != it.nextIndex(); }
LinkedList<String> elements = ...; //to avoid the slow ArrayList.remove() int k = elements.size() - count; //elements to select/delete int remaining = elements.size(); //elements remaining to be iterated for (Iterator i = elements.iterator(); k > 0 && i.hasNext(); remaining--) { i.next(); if (random.nextInt(remaining) < k) { //or (random.nextDouble() < (double)k/remaining) i.remove(); k--; } }