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:net.drgnome.virtualpack.util.Util.java

public static <T> T[] merge(T[]... objects) {
        ArrayList<T> list = new ArrayList<T>();
        for (T[] array : objects) {
            if (array == null) {
                continue;
            }// w ww .  j  av a 2 s . c o m
            for (T obj : array) {
                if (obj == null) {
                    continue;
                }
                if (!list.contains(obj)) {
                    list.add(obj);
                }
            }
        }
        return list.toArray((T[]) Array.newInstance(objects[0].getClass().getComponentType(), list.size()));
    }

From source file:mase.neat.NEATSerializer.java

public static NEATNeuralNet deserialize(double[] weights) {
    ArrayList<Gene> genes = new ArrayList<>();
    for (int i = 0; i < weights.length;) {
        double type = weights[i++];
        if (type == NODE) {
            int id = (int) (double) weights[i++];
            double sigF = weights[i++];
            int t = (int) (double) weights[i++];
            double bias = weights[i++];
            genes.add(new NEATNodeGene(0, id, sigF, t, bias));
        } else if (type == LINK) {
            boolean enabled = weights[i++] == 1d;
            int from = (int) (double) weights[i++];
            int to = (int) (double) weights[i++];
            double weight = weights[i++];
            genes.add(new NEATLinkGene(0, enabled, from, to, weight));
        }/*from   w ww. j av  a 2 s.  c  om*/
    }
    Gene[] geneArray = new Gene[genes.size()];
    genes.toArray(geneArray);
    NEATChromosome chromo = new NEATChromosome(geneArray);
    NEATNetDescriptor descr = new NEATNetDescriptor(0, null);
    descr.updateStructure(chromo);
    NEATNeuralNet network = new NEATNeuralNet();
    network.createNetStructure(descr);
    network.updateNetStructure();
    return network;
}

From source file:com.mtgi.analytics.JdbcEventTestCase.java

/** 
 * transform the given dbunit table into one that can be used to meaningfully compare
 * event data, without raising false negatives due to differences in start date or minor
 * changes in XML syntax for the event_data column.
 * @throws DataSetException /*from   ww  w . ja  v  a 2 s.  c o m*/
 */
public static ITable normalizedEventTable(ITable table) throws DataSetException {
    ITableMetaData metaData = table.getTableMetaData();
    ArrayList<Column> columns = new ArrayList<Column>();
    for (Column c : metaData.getColumns()) {
        String name = c.getColumnName();
        //strip out time columns.
        //TODO: leave in duration, with which we can compare with a tolerance value?
        if ("EVENT_START".equals(name) || "DURATION_NS".equals(name))
            continue;
        //replace string data type comparator with an xmlunit-based comparator
        if ("EVENT_DATA".equals(name))
            c = new Column("EVENT_DATA",
                    new XmlStringDataType(c.getSqlTypeName(), c.getDataType().getSqlType()));
        columns.add(c);
    }
    return new CompositeTable(new DefaultTableMetaData(metaData.getTableName(),
            columns.toArray(new Column[columns.size()]), metaData.getPrimaryKeys()), table);
}

From source file:com.esri.core.geometry.GeometryEngine.java

/**
 * constructs the set-theoretic difference between an array of geometries
 * and another geometry. The dimension of the input geometry has to be equal
 * to or greater than that of any element in the array of "geometries".
 * //from   w w w .  j  av a2 s.  c  om
 * @param inputGeometries
 *            an array of geometry objects being subtracted
 * @param subtractor
 *            geometry object to subtract from
 * @return any array of geometry objects showing the difference
 * */
static Geometry[] difference(Geometry[] inputGeometries, Geometry subtractor,
        SpatialReference spatialReference) {
    OperatorDifference op = (OperatorDifference) factory.getOperator(Operator.Type.Difference);
    SimpleGeometryCursor inputGeometriesCursor = new SimpleGeometryCursor(inputGeometries);
    SimpleGeometryCursor subtractorCursor = new SimpleGeometryCursor(subtractor);
    GeometryCursor result = op.execute(inputGeometriesCursor, subtractorCursor, spatialReference, null);

    ArrayList<Geometry> resultGeoms = new ArrayList<Geometry>();
    Geometry g;
    while ((g = result.next()) != null) {
        resultGeoms.add(g);
    }
    Geometry[] resultarr = resultGeoms.toArray(new Geometry[0]);
    return resultarr;
}

From source file:Main.java

/**
 * return a 2d array with the value and its count. Can deal with multiples.
 * Values will be in the same order as array (with subsequent duplicate
 * values removed). */*from  w w  w .  jav a  2s . c o  m*/
 * 
 * @param array
 *            the array
 * @param verbose
 *            the verbose
 * @return the count
 */
public static Double[][] getCount(double[] array, boolean verbose) {
    ArrayList<Double[]> countArray = new ArrayList<Double[]>();
    double count = 0;
    for (int i = 0; i < array.length; ++i) {
        // check if list contains current value
        boolean unique = true;
        for (Double[] ia : countArray) {
            if (ia[0].doubleValue() == array[i]) {
                unique = false;
            }
        }
        // Count values
        if (unique) {
            count = 1;
            for (int j = i + 1; j < array.length; ++j) {
                if (array[i] == array[j]) {
                    count++;
                }
            }
            countArray.add(new Double[] { array[i], count });
        }
    }
    Double[][] doubleArray = new Double[countArray.size()][];

    doubleArray = countArray.toArray(doubleArray);
    if (verbose) {
        System.out.println("Value and count of value:\n" + Arrays.deepToString(doubleArray));
    }
    return doubleArray;
}

From source file:com.esri.core.geometry.GeometryEngine.java

/**
 * Constructs the set-theoretic intersection between an array of geometries
 * and another geometry./*from w  w w .j a v a 2s .  c o  m*/
 * 
 * @param inputGeometries
 *            An array of geometry objects.
 * @param geometry
 *            The geometry object.
 * @return Any array of geometry objects showing the intersection.
 */
static Geometry[] intersect(Geometry[] inputGeometries, Geometry geometry, SpatialReference spatialReference) {
    OperatorIntersection op = (OperatorIntersection) factory.getOperator(Operator.Type.Intersection);
    SimpleGeometryCursor inputGeometriesCursor = new SimpleGeometryCursor(inputGeometries);
    SimpleGeometryCursor intersectorCursor = new SimpleGeometryCursor(geometry);
    GeometryCursor result = op.execute(inputGeometriesCursor, intersectorCursor, spatialReference, null);

    ArrayList<Geometry> resultGeoms = new ArrayList<Geometry>();
    Geometry g;
    while ((g = result.next()) != null) {
        resultGeoms.add(g);
    }

    Geometry[] resultarr = resultGeoms.toArray(new Geometry[0]);
    return resultarr;
}

From source file:com.google.android.apps.santatracker.data.DestinationDbHelper.java

private static Destination.Photo[] processPhoto(String s) {
    if (s == null || s.isEmpty()) {
        return null;
    }/*from w  ww.  ja  v  a 2 s .  c o  m*/

    ArrayList<Destination.Photo> list = new ArrayList<>(5);

    try {
        JSONArray array = new JSONArray(s);
        for (int i = 0; i < array.length(); i++) {
            JSONObject json = array.getJSONObject(i);
            Destination.Photo photo = new Destination.Photo();
            photo.url = json.getString(APIProcessor.FIELD_PHOTO_URL);
            photo.attributionHTML = json.getString(APIProcessor.FIELD_PHOTO_ATTRIBUTIONHTML);

            list.add(photo);
        }

    } catch (JSONException e) {
        // ignore invalid values
    }
    return list.isEmpty() ? null : list.toArray(new Destination.Photo[list.size()]);

}

From source file:com.qtplaf.library.util.StringUtils.java

/**
 * Parse a comma delimited "XX","XX","XX" string, and return an array with its elements.
 *
 * @param strToParse The String to parse.
 * @return An array of strings that are the parts of the comma delimited string to parse.
 *///w  w w  .  j a  v  a2s  .c  o m
public static String[] parseCommaDelimitedString(String strToParse) {
    if (strToParse.length() == 0) {
        return new String[0];
    }
    int start = 0;
    while (strToParse.charAt(start) != '\"') {
        start++;
    }
    int end = strToParse.length() - 1;
    while (strToParse.charAt(end) != '\"') {
        end--;
    }
    strToParse = strToParse.substring(++start, end);
    ArrayList<String> list = new ArrayList<>();
    start = 0;
    while (true) {
        end = strToParse.indexOf("\",\"", start);
        if (end == -1) {
            list.add(strToParse.substring(start));
            break;
        }
        list.add(strToParse.substring(start, end));
        start = end + 3;
    }
    return list.toArray(new String[list.size()]);
}

From source file:eu.scape_project.pt.mapred.input.ControlFileInputFormat.java

/**
 * Finds input file references in the control line by looking 
 * into its toolspec.//  w  w w.  j  a v  a 2s.  c  o  m
 *
 * @param fs Hadoop filesystem handle
 * @param parser for parsing the control line
 * @param repo Toolspec repository
 * @return array of paths to input file references
 */
public static Path[] getInputFiles(FileSystem fs, CmdLineParser parser, Repository repo, String controlLine)
        throws IOException {
    parser.parse(controlLine);

    Command command = parser.getCommands()[0];
    String strStdinFile = parser.getStdinFile();
    // parse it, read input file parameters
    Tool tool = repo.getTool(command.getTool());

    ToolProcessor proc = new ToolProcessor(tool);
    Operation operation = proc.findOperation(command.getAction());
    if (operation == null)
        throw new IOException("operation " + command.getAction() + " not found");

    proc.setOperation(operation);
    proc.setParameters(command.getPairs());
    Map<String, String> mapInputFileParameters = proc.getInputFileParameters();
    ArrayList<Path> inFiles = new ArrayList<Path>();
    if (strStdinFile != null) {
        Path p = new Path(strStdinFile);
        if (fs.exists(p)) {
            inFiles.add(p);
        }
    }

    for (String fileRef : mapInputFileParameters.values()) {
        Path p = new Path(fileRef);
        if (fs.exists(p)) {
            if (fs.isDirectory(p)) {
                inFiles.addAll(getFilesInDir(fs, p));
            } else {
                inFiles.add(p);
            }
        }
    }
    return inFiles.toArray(new Path[0]);
}

From source file:org.tinymediamanager.scraper.entities.MediaGenres.java

/**
 * Iterates over all found languages and gets the "alternative name" of specified property
 * //from  w ww .  j a v a 2s .  co  m
 * @param propName
 *          the property
 * @return array of alternate names
 */
public static String[] loadAlternateNames(String propName) {
    ArrayList<String> alt = new ArrayList<>();
    for (Locale loc : getLanguages()) {
        if (loc == null || loc.getLanguage().equals("en")) {
            // English not needed, since it's in default properties
            // and for invalid languages (like NB) it will be null
            continue;
        }
        ResourceBundle b = ApiResourceBundle.getResourceBundle(loc);
        try {
            alt.add(loc.getLanguage() + "-" + b.getString("Genres." + propName)); // just genres
        } catch (Exception e) {
            // not found or localized - ignore
        }
    }
    return alt.toArray(new String[alt.size()]);
}