Example usage for java.util LinkedList indexOf

List of usage examples for java.util LinkedList indexOf

Introduction

In this page you can find the example usage for java.util LinkedList indexOf.

Prototype

public int indexOf(Object o) 

Source Link

Document

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Usage

From source file:Main.java

public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    lList.add("2");

    System.out.println(lList.indexOf("2"));

    System.out.println(lList.lastIndexOf("2"));

}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // get the index for "Chocolate"
    System.out.println("Index for Chocolate:" + list.indexOf("Hello"));

    // get the index for "Coffee"
    System.out.println("Index for Coffee:" + list.indexOf("Coffee"));
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // get the last index for "Hello"
    System.out.println("Index for Hello:" + list.lastIndexOf("Hello"));

    // get the index for "Coffee"
    System.out.println("Index for Coffee:" + list.indexOf("Coffee"));
}

From source file:jp.co.ctc_g.jse.vid.ViewId.java

private static void is(ViewId self, ViewIdStore store) {

    Args.checkNotNull(self);//from  www  .  j  a va 2  s.c  o  m
    self.freeze();
    synchronized (store.semaphore()) {
        LinkedList<ViewId> ids = store.find();
        assert ids != null;
        if (ids.contains(self)) {
            int index = ids.indexOf(self);
            for (int i = ids.size() - 1; i > index; i--) {
                ids.remove(i);
            }
            if (OVERRIDE_THE_SAME_ONE) {
                ids.set(index, self);
            }
        } else {
            ids.add(self);
        }
        store.store(ids);
    }
}

From source file:util.allocation.java

public static LinkedList<HOST> recalculateSpaceForHOSTList(LinkedList<HOST> HOSTList,
        String currentHOSTSolutionID, VM currentVM) {
    HOST currentHOST = getHOSTByID(currentHOSTSolutionID, HOSTList);

    float currentHOSTVolume = currentHOST.getVolume();
    String currentHOStVolumeUnit = currentHOST.getVolumeUnit();

    float currentVMVolume = currentVM.getVolume();
    float newVolume = currentHOSTVolume - currentVMVolume;

    String newItem = Float.toString(newVolume) + "_" + currentHOStVolumeUnit;

    HOSTList.get(HOSTList.indexOf(currentHOST)).getServiceDescription().replace("volume", newItem);

    return HOSTList;

}

From source file:eu.uqasar.model.qmtree.QMTreeNode.java

@Override
public boolean canChangePositionWithPreviousSibling(boolean changeParents) {
    LinkedList<QMTreeNode> directSiblings = (LinkedList<QMTreeNode>) getMutableSiblings();
    int currentIndex = directSiblings.indexOf(this);
    if (currentIndex > 0) {
        // switch currently selected node with the previous one
        return true;
    } else if (currentIndex == 0 && changeParents) {
        // add currently selected node as last entry to the previous
        // parent sibling
        LinkedList<QMTreeNode> parentSiblings = (LinkedList<QMTreeNode>) this.getParent().getMutableSiblings();
        int parentIndex = parentSiblings.indexOf(this.getParent());
        return parentIndex > 0;
    }//from   w  w w .ja va  2s  . c  om
    return false;
}

From source file:eu.uqasar.model.qmtree.QMTreeNode.java

@Override
public boolean canChangePositionWithNextSibling(boolean changeParents) {
    LinkedList<QMTreeNode> directSiblings = (LinkedList<QMTreeNode>) getMutableSiblings();
    int currentIndex = directSiblings.indexOf(this);
    int newIndex = currentIndex + 1;
    if (newIndex < directSiblings.size()) {
        // switch currently selected node with the next one
        return true;
    } else if (newIndex >= directSiblings.size() && changeParents) {
        // add currently selected node as first entry to the next parent
        // sibling
        LinkedList<QMTreeNode> parentSiblings = (LinkedList<QMTreeNode>) this.getParent().getMutableSiblings();
        int parentIndex = parentSiblings.indexOf(this.getParent());
        int newParentIndex = parentIndex + 1;
        if (newParentIndex < parentSiblings.size()) {
            return true;
        }/*from w ww  . j  av  a2s.co m*/
    }
    return false;
}

From source file:eu.uqasar.model.qmtree.QMTreeNode.java

@SuppressWarnings("unchecked")
@Override/*from   w  ww  .  jav  a 2s.co  m*/
public boolean changePositionWithNextSibling(boolean changeParents) {
    LinkedList<QMTreeNode> directSiblings = (LinkedList<QMTreeNode>) getMutableSiblings();
    int currentIndex = directSiblings.indexOf(this);
    int newIndex = currentIndex + 1;
    if (newIndex < directSiblings.size()) {
        // switch currently selected node with the next one
        QMTreeNode movedNode = directSiblings.remove(currentIndex);
        directSiblings.add(newIndex, movedNode);
        getParent().setChildren(directSiblings);
        logger.info(String.format("Moving %s from index %s to %s", this, currentIndex, newIndex));
        return true;
    } else if (newIndex >= directSiblings.size() && changeParents) {
        // add currently selected node as first entry to the next parent
        // sibling
        LinkedList<QMTreeNode> parentSiblings = (LinkedList<QMTreeNode>) this.getParent().getMutableSiblings();
        int parentIndex = parentSiblings.indexOf(this.getParent());
        int newParentIndex = parentIndex + 1;
        if (newParentIndex < parentSiblings.size()) {
            QMTreeNode oldParent = this.getParent();
            LinkedList<QMTreeNode> oldParentChildren = (LinkedList) oldParent.getChildren();
            oldParentChildren.removeLast();
            oldParent.setChildren(oldParentChildren);

            QMTreeNode newParent = parentSiblings.get(newParentIndex);
            logger.info(String.format("Moving %s from parent %s to %s", this, this.getParent(), newParent));
            this.setParent(newParent);
            return true;
        }
    }
    return false;
}

From source file:eu.uqasar.model.qmtree.QMTreeNode.java

@SuppressWarnings("unchecked")
@Override/*ww w .j  a v  a2s. c  o m*/
public boolean changePositionWithPreviousSibling(boolean changeParents) {
    LinkedList<QMTreeNode> directSiblings = (LinkedList<QMTreeNode>) getMutableSiblings();
    int currentIndex = directSiblings.indexOf(this);
    int newIndex = currentIndex - 1;
    if (currentIndex > 0) {
        // switch currently selected node with the previous one
        QMTreeNode movedNode = directSiblings.remove(currentIndex);
        directSiblings.add(newIndex, movedNode);
        getParent().setChildren(directSiblings);
        logger.info(String.format("Moving %s from index %s to %s", this, currentIndex, newIndex));
        return true;
    } else if (currentIndex == 0 && changeParents) {
        // add currently selected node as last entry to the previous
        // parent sibling
        LinkedList<QMTreeNode> parentSiblings = (LinkedList<QMTreeNode>) this.getParent().getMutableSiblings();
        int parentIndex = parentSiblings.indexOf(this.getParent());
        int newParentIndex = parentIndex - 1;
        if (parentIndex > 0) {
            QMTreeNode oldParent = this.getParent();
            LinkedList<QMTreeNode> oldParentChildren = (LinkedList) oldParent.getChildren();
            oldParentChildren.remove(0);
            oldParent.setChildren(oldParentChildren);

            QMTreeNode newParent = parentSiblings.get(newParentIndex);
            logger.info(String.format("Moving %s from parent %s to %s", this, this.getParent(), newParent));
            this.setParent(newParent);
            return true;
        }
    }
    return false;
}

From source file:net.erdfelt.android.sdkfido.configer.ConfigCmdLineParser.java

public void parse(String[] args) throws CmdLineParseException {
    LinkedList<String> arglist = new LinkedList<String>();
    arglist.addAll(Arrays.asList(args));

    // Quick Help
    if (arglist.contains("--" + OPT_HELP)) {
        usage();//from   ww  w  . j  a  va  2 s . c o m
        return;
    }

    // Configuration File Option
    int idx = arglist.indexOf("--" + OPT_CONFIG);
    if (idx >= 0) {
        if (idx + 1 > arglist.size()) {
            throw new CmdLineParseException("Expected <File> parameter for option: --" + OPT_CONFIG);
        }
        String value = arglist.get(idx + 1);
        File file = (File) ConvertUtils.convert(value, File.class);

        this.configer.setPersistFile(file);

        arglist.remove(idx + 1);
        arglist.remove(idx);
    }

    // Save Options Option
    boolean saveOptions = false;

    idx = arglist.indexOf("--" + OPT_SAVE);
    if (idx >= 0) {
        saveOptions = true;
        arglist.remove(idx);
    }

    // Restore from persist first.
    try {
        configer.restore();
    } catch (IOException e) {
        throw new CmdLineParseException("Unable to load configuration: " + e.getMessage(), e);
    }

    // Set values from command line now.
    String value;
    ListIterator<String> iter = arglist.listIterator();
    while (iter.hasNext()) {
        String arg = iter.next();
        if (arg.startsWith("--")) {
            // Its an option.
            String optname = arg.substring(2);

            Configurable cfgrbl = configer.getConfigurable(optname);

            if (cfgrbl == null) {
                throw new CmdLineParseException("Invalid Option: " + arg);
            }

            if (!iter.hasNext()) {
                throw new CmdLineParseException(
                        "Expected <" + cfgrbl.getType() + "> parameter for option: " + arg);
            }
            value = iter.next();
            configer.setValue(optname, value);
            continue; // process next arg
        }

        // All others are considered args.
        addToRawArgs(arg);
    }

    // Save options (if specified)
    if (saveOptions) {
        try {
            configer.persist();
        } catch (IOException e) {
            throw new CmdLineParseException("Unable to save configuration: " + e.getMessage(), e);
        }
    }
}