Example usage for java.util ArrayList size

List of usage examples for java.util ArrayList size

Introduction

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

Prototype

int size

To view the source code for java.util ArrayList size.

Click Source Link

Document

The size of the ArrayList (the number of elements it contains).

Usage

From source file:Main.java

public static <T> ArrayList<T> rand(ArrayList<T> population, int nSamplesNeeded) {
    Random r = new Random();
    ArrayList<T> ret = new ArrayList<T>();

    if (nSamplesNeeded > population.size() / 2) {
        ArrayList<T> original = new ArrayList<T>();
        original = population;//from   www.ja  va  2  s. c o  m

        while (nSamplesNeeded > 0) {
            int rand = r.nextInt(original.size());
            if (rand < nSamplesNeeded) {
                ret.add(original.get(rand));
                original.remove(rand);
                nSamplesNeeded--;
            }
        }
        original.clear();
    } else
        ret = shuffle(population, nSamplesNeeded);

    return ret;
}

From source file:Main.java

public static <T> ArrayList<T> shuffle(ArrayList<T> population, int sample) {
    ArrayList<T> newList = new ArrayList<T>();
    ArrayList<T> ret = new ArrayList<T>();
    newList.addAll(population);//from   ww w.jav a2s  . c o m

    Collections.shuffle(newList);
    ret.addAll(newList);
    for (int i = sample; i < ret.size(); i++) {
        ret.remove(i);
        i--;
    }
    newList.clear();
    return ret;
}

From source file:Main.java

public static String[] getJSONObjectKeys(JSONObject inputObject) {
    Iterator keys = inputObject.keys();
    ArrayList<String> objectKeys = new ArrayList<String>();

    while (keys != null && keys.hasNext()) {
        objectKeys.add((String) keys.next());
    }/*from  w ww.j  a  v a  2  s .c  om*/
    String[] returnArray = new String[objectKeys.size()];
    return objectKeys.toArray(returnArray);
}

From source file:Main.java

public static <T> ArrayList<T> distinctList(ArrayList<T> arg) {
    ArrayList<T> distinct = new ArrayList<T>();
    if (null != arg) {
        distinct = new ArrayList<T>();
        distinct.addAll(arg);//from   www. j a v  a 2s. com
        for (int i = 0; i < distinct.size(); i++) {
            T t1 = distinct.get(i);
            for (int j = i + 1; j < distinct.size(); j++) {
                T t2 = distinct.get(j);
                if (t1.equals(t2)) {
                    distinct.remove(j);
                    j--;
                }
            }
        }
    }
    return distinct;
}

From source file:imperial.modaclouds.monitoring.sda.weka.CreateArff.java

public static double[] convertDoubles(ArrayList<String> strings) {
    double[] ret = new double[strings.size()];
    for (int i = 0; i < strings.size(); i++) {
        ret[i] = Double.valueOf(strings.get(i));
    }/*w  w  w.j  a  v  a 2s.  c om*/
    return ret;
}

From source file:com.flexdesktop.connections.restfulConnection.java

public static void soutMatrix(ArrayList<ArrayList<String>> outJson) {
    for (ArrayList<String> outJson1 : outJson) {
        for (int j = 0; j < outJson1.size(); j++) {
            System.out.println(outJson1.get(j));
        }//  ww  w .j  a  v  a 2  s .  co  m
    }
}

From source file:com.safecell.model.SCLicense.java

public static void insertOrUpdateLicenseKey(ArrayList<SCLicense> licenses, Context context) {

    boolean licenseIdPresent = false;

    for (int i = 0; i < licenses.size(); i++) {
        int id = licenses.get(i).getId();
        LicenseRepository licenseRepository = new LicenseRepository(context);
        licenseIdPresent = licenseRepository.licensesIdPresent(String.valueOf(id));

        if (licenseIdPresent) {
            licenseRepository.updateQuery(licenses.get(i));
            //Log.v("Safecell :"+"update", "update");
        } else {//from  ww w  .  java  2s  .  c  o m
            licenseRepository.insertQuery(licenses.get(i));
            //Log.v("Safecell :"+"insert", "insert");
        }
    }

}

From source file:Main.java

public static int countDiffElmts(ArrayList<String> u, ArrayList<String> v) {
    ArrayList<String> listOne = new ArrayList<String>();
    ArrayList<String> listTwo = new ArrayList<String>();
    listOne.addAll(u);//from w  w w  . j a  v  a 2  s . c om
    listOne.retainAll(v);
    listTwo.addAll(u);
    listTwo.addAll(v);
    listTwo.removeAll(listOne);
    return listTwo.size();
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static void mapToXml(Map map, StringBuffer sb) {
    Set set = map.keySet();/*www  .j  a  va  2s. co  m*/
    for (Iterator it = set.iterator(); it.hasNext();) {
        String key = (String) it.next();
        Object value = map.get(key);
        if (null == value)
            value = "";
        if (value instanceof List) {
            ArrayList list = (ArrayList) map.get(key);
            // sb.append("<" + key + ">");
            for (int i = 0; i < list.size(); i++) {
                sb.append("<" + key + ">");
                // Object listi = list.get(i);
                if (list.get(i) instanceof HashMap) {
                    HashMap hm = (HashMap) list.get(i);
                    // sb.append("<" + key + ">");
                    mapToXml(hm, sb);
                    // sb.append("</" + key + ">");
                } else {
                    // sb.append("<" + key + ">" + list.get(i) + "</" + key
                    // + ">");
                    sb.append(list.get(i));
                }
                // else
                // if(listi.getClass().getName().equals("java.util.ArrayList")){
                // sb.append("<" + key + ">" + "??" + "</" + key + ">");}

                sb.append("</" + key + ">");
            }
            // sb.append("</" + key + ">");

        } else {
            if (value instanceof HashMap) {
                sb.append("<" + key + ">");
                mapToXml((HashMap) value, sb);
                sb.append("</" + key + ">");
            } else {
                sb.append("<" + key + ">" + value + "</" + key + ">");
            }

        }

    }
}

From source file:Main.java

public static String[] join(String[] left, String[] right) {
    ArrayList<String> list = new ArrayList<String>();
    list.addAll(Arrays.asList(left));
    list.addAll(Arrays.asList(right));
    return (String[]) list.toArray(new String[list.size()]);
}