canonical example by Tabnine
private void usingArrayList() { ArrayList<String> list = new ArrayList<>(Arrays.asList("cat", "cow", "dog")); list.add("fish"); int size = list.size(); // size = 4 list.set(size - 1, "horse"); // replacing the last element to "horse" String removed = list.remove(1); // removed = "cow" String second = list.get(1); // second = "dog" }
private ArrayList<Integer> rotating_random_range(int amt) { ArrayList<Integer> range = new ArrayList<>(); for (int i = 0; i < amt; i++) { range.add(i); } ArrayList<Integer> rtn = new ArrayList<>(); for (int i = 0; i < amt; i++) { int index = (int) (Math.random() * range.size()); rtn.add(range.remove(index)); } return rtn; }
/** Concurrent modification bug */ public static void main(String[] args) { ArrayList<String> namesList = new ArrayList<String>(); namesList.add("Kelly"); namesList.add("John"); namesList.add("Peter"); namesList.add("Rose"); for (Iterator<String> i = namesList.iterator(); i.hasNext(); ) { String name = i.next(); if (name.equals("Peter")) { namesList.remove(name); } else { System.out.println(name); } } } }
System.out.println("******* Distinct shapes found using AbstractionGrid *******"); Iterator dit = boundarySetsStep1.iterator(); while (dit.hasNext()) { set.printAsGrid(); System.out.println("******* Same set of shapes after processing them by filling *******"); ArrayList boundarySetsStep2 = new ArrayList(); Iterator boundarySetIt = boundarySetsStep1.iterator(); while (boundarySetIt.hasNext()) { System.out.println("-----------------------------------"); ArrayList open = new ArrayList(); ArrayList closed = new ArrayList(); if(mixed.size() > 0 && closed.size() > 0) { boundarySetsStep2.remove(set); boundarySetsStep2.addAll(set.breakIntoDistinctBoundaries(workGrid)); while(sets.hasNext()){ CellSet set = (CellSet) sets.next(); boundarySetsStep2.remove(set); boundarySetsStep2.addAll(set.breakTrulyMixedBoundaries(workGrid));
@Override public void remove() { if (!this.nextWasCalled) { throw new IllegalStateException("No element to remove"); } this.nextWasCalled = false; List<HStoreFile> src = components.get(currentComponent); if (src instanceof ImmutableList<?>) { src = new ArrayList<>(src); components.set(currentComponent, src); } src.remove(indexWithinComponent); --size; --indexWithinComponent; if (src.isEmpty()) { components.remove(currentComponent); // indexWithinComponent is already -1 here. } } }
/** * Adds a new weighted value. * * @param weight The weight of the new observation. * @param value The value of the new observation. */ public void addSample(int weight, float value) { ensureSortedByIndex(); Sample newSample = recycledSampleCount > 0 ? recycledSamples[--recycledSampleCount] : new Sample(); newSample.index = nextSampleIndex++; newSample.weight = weight; newSample.value = value; samples.add(newSample); totalWeight += weight; while (totalWeight > maxWeight) { int excessWeight = totalWeight - maxWeight; Sample oldestSample = samples.get(0); if (oldestSample.weight <= excessWeight) { totalWeight -= oldestSample.weight; samples.remove(0); if (recycledSampleCount < MAX_RECYCLED_SAMPLES) { recycledSamples[recycledSampleCount++] = oldestSample; } } else { oldestSample.weight -= excessWeight; totalWeight -= excessWeight; } } }
/** * Removes a collection from the those being decorated in this composite. * * @param coll collection to be removed */ public void removeComposited(Collection coll) { ArrayList list = new ArrayList(this.all.length); list.addAll(Arrays.asList(this.all)); list.remove(coll); this.all = (Collection[]) list.toArray(new Collection[list.size()]); }
public void run() { String maryBase = System.getProperty("mary.base"); System.out.println("Installing " + name + "-" + version + " in " + maryBase + "..."); ArrayList<String> files = new ArrayList<String>(); try { ZipFile zipfile = new ZipFile(archiveFile); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); files.add(entry.getName()); // add to installed filelist; rely on uninstaller retaining shared files File newFile = new File(maryBase + "/" + entry.getName()); if (entry.isDirectory()) { System.err.println("Extracting directory: " + entry.getName()); newFile.mkdir(); } else { files.remove(entry.getName()); System.err.println("NOT overwriting existing newer file: " + entry.getName()); continue;
ArrayList<String> list = new ArrayList<String>(); JSONArray jsonArray = (JSONArray)jsonObject; int len = jsonArray.length(); if (jsonArray != null) { for (int i=0;i<len;i++){ list.add(jsonArray.get(i).toString()); } } //Remove the element from arraylist list.remove(position); //Recreate JSON Array JSONArray jsArray = new JSONArray(list);