Example usage for java.util ArrayList toArray

List of usage examples for java.util ArrayList toArray

Introduction

In this page you can find the example usage for java.util ArrayList toArray.

Prototype

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) 

Source Link

Document

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Usage

From source file:Main.java

/**
 * Get an array of specific attributes for specific elements
 * in a Node tree. If the node is a Document, use the document
 * element as the starting point./* w ww .j  a v  a  2 s.  co  m*/
 * @param node the top of the tree to search.
 * @param nodeName the element names to include in the search.
 * @param attrName the name of the attributes to include.
 * @return the array of attribute values natching the criteria.
 */
public static String[] getAttributeValues(Node node, String nodeName, String attrName) {
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return new String[0];
    NodeList nodeList = ((Element) node).getElementsByTagName(nodeName);
    ArrayList list = new ArrayList();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Attr attr = ((Element) nodeList.item(i)).getAttributeNode(attrName);
        if (attr != null)
            list.add(attr.getValue());
    }
    String[] values = new String[list.size()];
    return (String[]) list.toArray(values);
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//from  ww  w .j av a 2 s  .co m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);
        }
    }
    return al.toArray(new Element[0]);
}

From source file:Main.java

/**
 * returns an arrayList of language codes that are active.
 * /*from   www  . j a  v a  2  s .c o  m*/
 * @param bs
 * @return
 */
public static String[] getSelectedLangCodes(Context context, int[] indexes, boolean[] selectedItems,
        int codeResourceId) {
    ArrayList<String> codes = new ArrayList<String>();
    Resources res = context.getResources();
    String[] allCodes = res.getStringArray(codeResourceId);
    for (int i = 0; i < indexes.length; i++) {
        if (selectedItems[i]) {
            codes.add(allCodes[indexes[i]]);
        }
    }
    return codes.toArray(new String[codes.size()]);
}

From source file:Main.java

public static Node[] getChildrenNamed(Node node, String name) {
    ArrayList v = new ArrayList();

    NodeList nList = node.getChildNodes();
    for (int i = 0; i < nList.getLength(); i++) {
        Node n = nList.item(i);//from ww  w .  ja  va2s .c  o m
        if (n.getNodeName().equalsIgnoreCase(name)) {
            v.add(n);
        }
    }

    Node[] nodeArray = new Node[v.size()];
    nodeArray = (Node[]) v.toArray(nodeArray);
    return nodeArray;
}

From source file:com.cloudera.flume.shell.CommandBuilder.java

/**
 * This takes a single string and parses it into a Command.
 *//*from w w w  .  ja v a  2  s .  c  o m*/
public static Command parseLine(String s) throws RecognitionException {
    try {
        CommonTree cmd = (CommonTree) getShellCmdParser(s).line().getTree();
        CommonTree ast = (CommonTree) cmd.getChild(0);
        String command = toString(ast);
        cmd.deleteChild(0);
        ArrayList<String> lst = new ArrayList<String>();
        for (int i = 0; i < cmd.getChildCount(); i++) {
            String tok = toString((CommonTree) cmd.getChild(i));
            lst.add(tok);
        }
        return new Command(command, lst.toArray(new String[0]));

    } catch (RecognitionException e) {
        LOG.debug("Failed to parse line, '" + s + "' because of " + e.getMessage(), e);
        throw e;
    } catch (RuntimeException rex) {
        // right now lexer errors are RTE's
        LOG.debug("Failed to lex '" + s + "' because of " + rex.getMessage(), rex);
        throw new CommandLineException(rex);
    }
}

From source file:com.dnielfe.manager.utils.SortUtils.java

public static ArrayList<String> sortList(ArrayList<String> content, String current) {
    int len = content != null ? content.size() : 0;
    int index = 0;
    String[] items = new String[len];
    content.toArray(items);

    switch (Settings.mSortType) {
    case SORT_ALPHA:
        Arrays.sort(items, Comparator_ALPH);
        content.clear();/*from   w  w w  . j  ava  2s  .c  om*/

        for (String a : items) {
            content.add(a);
        }
        break;
    case SORT_SIZE:
        Arrays.sort(items, Comparator_SIZE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add((String) a);
        }
        break;
    case SORT_TYPE:
        Arrays.sort(items, Comparator_TYPE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;

    case SORT_DATE:
        Arrays.sort(items, Comparator_DATE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;
    }

    return content;
}

From source file:com.lovejoy777sarootool.rootool.utils.SortUtils.java

public static void sortList(ArrayList<String> content, String current) {
    int len = content != null ? content.size() : 0;
    int index = 0;
    String[] items = new String[len];
    content.toArray(items);

    switch (Settings.mSortType) {
    case SORT_ALPHA:
        Arrays.sort(items, Comparator_ALPH);
        content.clear();// w w w  .  java2  s. c  om

        Collections.addAll(content, items);
        break;
    case SORT_SIZE:
        Arrays.sort(items, Comparator_SIZE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;
    case SORT_TYPE:
        Arrays.sort(items, Comparator_TYPE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;

    case SORT_DATE:
        Arrays.sort(items, Comparator_DATE);
        content.clear();

        for (String a : items) {
            if (new File(current + "/" + a).isDirectory())
                content.add(index++, a);
            else
                content.add(a);
        }
        break;
    }

    if (Settings.reverseListView()) {
        Collections.reverse(content);
    }
}

From source file:Main.java

public static File[] getSavegames(File folder) {
    File[] files = folder.listFiles();
    ArrayList<File> saveFiles = new ArrayList<File>();
    if (files != null) {
        for (final File fileEntry : files) {
            if (fileEntry.isFile()) {
                if (fileEntry.getName().toLowerCase().endsWith(".lsd")) {
                    saveFiles.add(fileEntry);
                }//w ww.  j  a  va  2  s  .co m
            }
        }
    }
    return saveFiles.toArray(new File[saveFiles.size()]);
}

From source file:info.dolezel.fatrat.plugins.helpers.NativeHelpers.java

public static Class[] findAnnotatedClasses(String packageName, Class annotation)
        throws IOException, ClassNotFoundException {
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = loader.getResources(path);
    Set<File> dirs = new HashSet<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
    }/*from  w w w.ja v  a  2s  . co m*/
    for (URL url : loader.getURLs()) {
        dirs.add(new File(url.getFile()));
    }

    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName, annotation));
    }

    return classes.toArray(new Class[classes.size()]);
}

From source file:Main.java

public static String[] getContextWindow(String[] a, int index, int windowSize) {
    ArrayList<String> toReturnAL = new ArrayList<String>();

    int begin = Math.max(0, index - windowSize);
    int end = Math.min(a.length, index + windowSize + 1);

    for (int i = begin; i < end; i++) {
        if (i == index)
            toReturnAL.add("[h]" + a[i] + "[/h]");
        else/*from   w w w . j  a  va2  s.  c o m*/
            toReturnAL.add(a[i]);
    }

    return toReturnAL.toArray(new String[0]);
}