List of usage examples for java.util ArrayList add
public boolean add(E e)
From source file:Person.java
public static void main(String args[]) { ArrayList<Person> names = new ArrayList<Person>(); names.add(new Person("E", "T")); names.add(new Person("A", "G")); names.add(new Person("B", "H")); names.add(new Person("C", "J")); Iterator iter1 = names.iterator(); while (iter1.hasNext()) { System.out.println(iter1.next()); }/*from ww w .j a va 2s . c o m*/ Collections.sort(names, new PersonComparator()); Iterator iter2 = names.iterator(); while (iter2.hasNext()) { System.out.println(iter2.next()); } }
From source file:Person.java
public static void main(String args[]) { ArrayList<Person> names = new ArrayList<Person>(); names.add(new Person("E", "T")); names.add(new Person("A", "G")); names.add(new Person("B", "H")); names.add(new Person("C", "J")); Iterator iter1 = names.iterator(); while (iter1.hasNext()) { System.out.println(iter1.next()); }/* www .j a v a 2 s . com*/ System.out.println("Before sorting"); Collections.sort(names, new PersonComparator()); Iterator iter2 = names.iterator(); while (iter2.hasNext()) { System.out.println(iter2.next()); } }
From source file:Main.java
public static void main(String[] args) { String givenstring = "this is a test this is a another test"; String[] words = givenstring.split(" "); ArrayList<String> arr = new ArrayList<String>(); for (int i = 0; i < words.length; i++) { arr.add(words[i]); }//from w w w. j av a 2s . com while (arr.size() != 0) { String word = arr.get(0); int frequency = Collections.frequency(arr, word); arr.removeAll(Collections.singleton(word)); System.out.println(word + frequency); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); writer.setPdfVersion(PdfWriter.VERSION_1_5); document.open();// www . j a v a2 s. co m PdfLayer a1 = new PdfLayer("answer 1", writer); a1.setOn(false); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); PdfContentByte cb = writer.getDirectContent(); cb.beginText(); cb.setTextMatrix(50, 790); cb.setLeading(24); cb.setFontAndSize(bf, 18); cb.newlineShowText("This is a question?"); cb.beginLayer(a1); cb.setRGBColorFill(0xFF, 0x00, 0x00); cb.newlineShowText("This is the answer"); cb.resetRGBColorFill(); cb.endLayer(); cb.endText(); ArrayList stateOn = new ArrayList(); stateOn.add("ON"); stateOn.add(a1); PdfAction actionOn = PdfAction.setOCGstate(stateOn, true); ArrayList stateOff = new ArrayList(); stateOff.add("OFF"); stateOff.add(a1); PdfAction actionOff = PdfAction.setOCGstate(stateOff, true); ArrayList stateToggle = new ArrayList(); stateToggle.add("Toggle"); stateToggle.add(a1); PdfAction actionToggle = PdfAction.setOCGstate(stateToggle, true); Phrase p = new Phrase("Change the state of the answers:"); Chunk on = new Chunk(" on ").setAction(actionOn); p.add(on); Chunk off = new Chunk("/ off ").setAction(actionOff); p.add(off); Chunk toggle = new Chunk("/ toggle").setAction(actionToggle); p.add(toggle); document.add(p); document.close(); }
From source file:CompTest.java
public static void main(String args[]) { ArrayList u2 = new ArrayList(); u2.add("Beautiful Day"); u2.add("Stuck In A Moment You Can't Get Out Of"); u2.add("Elevation"); u2.add("Walk On"); u2.add("Kite"); u2.add("In A Little While"); u2.add("Wild Honey"); u2.add("Peace On Earth"); u2.add("When I Look At The World"); u2.add("New York"); u2.add("Grace"); Comparator comp = Comparators.stringComparator(); Collections.sort(u2, comp);//from w w w . jav a 2 s .c om System.out.println(u2); Arrays.sort(args, comp); System.out.print("["); for (int i = 0, n = args.length; i < n; i++) { if (i != 0) System.out.print(", "); System.out.print(args[i]); } System.out.println("]"); }
From source file:net.anymeta.Test.java
/** * @param args/*from w w w. ja v a2s .c o m*/ */ public static void main(String[] arg) throws AnyMetaRegistryException, AnyMetaException, JSONException { HashMap<String, Object> args; // Load the API AnyMetaAPI api = AnyMetaAPI.fromRegistry("pluto.local"); // Get information for the currently logged in user. JSONObject o = (JSONObject) api.doMethod("anymeta.user.info"); System.out.println("Logged in as " + o.getString("title")); // args = new HashMap<String, Object>(); args.put("q_kind", "PERSON"); JSONArray a = (JSONArray) api.doMethod("query.execute", args); System.out.println(a.toString()); System.out.println(a.length()); // Lookup an RFID tag. args = new HashMap<String, Object>(); ArrayList<String> ids = new ArrayList<String>(); ids.add("65"); ids.add("94"); args.put("ids", ids); args.put("predicate", "KNOWS"); o = (JSONObject) api.doMethod("contact.link", args); System.out.println(o.toString()); // Upload an image args = new HashMap<String, Object>(); args.put("mime", "image/jpeg"); args.put("data", "@C:\\bla.jpg"); o = (JSONObject) api.doMethod("anymeta.attachment.create", args); System.out.println(o.toString()); }
From source file:MainClass.java
public static void main(String[] args) { ArrayList myList = new ArrayList(5); for (int i = 0; i < 5; i++) { myList.add(new Integer(i)); }//w ww . ja v a 2 s .c om System.out.println("List contains " + myList.size() + " elements"); Integer int2 = new Integer(2); System.out.println("List contains Integer(2): " + myList.contains(int2)); System.out.println("Integer(2) is at index " + myList.indexOf(int2)); myList.set(2, new Integer(99)); System.out.println("Get element at index 2: " + myList.get(2)); myList.ensureCapacity(15); for (int i = 10; i < 15; i++) { myList.add(new Integer(i)); } myList.subList(10, 15).clear(); myList.trimToSize(); System.out.println(myList); }
From source file:Main.java
public static void main(String args[]) { // create an empty array list ArrayList<StringBuilder> arrlist1 = new ArrayList<StringBuilder>(); // use add for new value arrlist1.add(new StringBuilder("from java2s.com")); // using clone to affect the objects pointed to by the references. ArrayList arrlist2 = (ArrayList) arrlist1.clone(); // appending the string StringBuilder strbuilder = arrlist1.get(0); strbuilder.append("tutorials"); System.out.println(arrlist1); System.out.println(arrlist2); }
From source file:ArrayListTest.java
public static void main(String[] args) { // fill the staff array list with three Employee objects ArrayList<Employee> staff = new ArrayList<Employee>(); staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15)); staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1)); staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15)); // raise everyone's salary by 5% for (Employee e : staff) e.raiseSalary(5);//w w w . jav a 2 s . c o m // print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); }
From source file:Main.java
public static void main(String[] args) { ArrayList<Integer> arrlist = new ArrayList<Integer>(5); // use add() method to add elements in the list arrlist.add(20); arrlist.add(30);//from w w w . j a va 2s. c o m arrlist.add(10); arrlist.add(50); System.out.println(arrlist); // finding size of this list int retval = arrlist.size(); System.out.println("List consists of " + retval + " elements"); arrlist.clear(); retval = arrlist.size(); System.out.println("list consists of " + retval + " elements"); }