package tutorial; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Scanner; public class bucky { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x1 = sc.nextInt(); System.out.println(Integer.bitCount(x1)); String s = "buckey robertkrsv fsgfg"; System.out.println(s.indexOf('k', 5)); // found the first index of k // count over the 5 elem System.out.println(s.indexOf('l')); System.out.println(s.indexOf("rob")); String a = "bacon "; String b = " monster "; System.out.println(a + b); System.out.println(a.concat(b)); System.out.println(a.replace('b', 'f')); System.out.println(b.toUpperCase()); System.out.println(b.trim()); System.out.println(fact(5)); String[] things = { "laser", "pie", "eggs", "hats" }; List list = new ArrayList(); // add array items to the list\ for (String x : things) { list.add(x); } for (String x : list) { System.out.println(x); } String[] morethings = { "pie", "eggs", "rebeca" }; List list2 = new ArrayList(); for (String y : morethings) { list2.add(y); } for (int i = 0; i < list.size(); i++) { System.out.printf("%s ", list.get(i)); } editlist(list, list2); System.out.println(); for (int i = 0; i < list.size(); i++) { System.out.printf("%s ", list.get(i)); } String[] thins = { "apples", "noobs", "pwnge", "bacon" }; List list1 = new LinkedList(); for (String x : thins) { list1.add(x); } String[] tns2 = { "bacon", "sausage", "goats", "harrypotter" }; List lst2 = new LinkedList(); for (String y : tns2) { lst2.add(y); } list1.addAll(lst2); lst2 = null; System.out.println(); printMe(list1); System.out.println(); removeStuff(list1, 0, 3); printMe(list1); reverseMe(list1);// reverse averything and print it out String[] stuff = { "babies", " watermelon", "melons" }; LinkedList thelist = new LinkedList( Arrays.asList(stuff)); thelist.add("pumking"); thelist.addFirst("firstthing"); thelist.addLast("me"); // convert back to an array stuff = thelist.toArray(new String[thelist.size()]); for (String r : stuff) { System.out.printf("%s ", r); } String[] crap = { "apples", "lemons", "geese", "bacon", "youtube" }; List l1 = Arrays.asList(crap); Collections.sort(l1); // in alphabetical order System.out.println( l1); // entire list as a string Collections.sort(l1, Collections.reverseOrder()); // what do yuo want to // sort and how do // you want to sort System.out.println(l1);//ciudat face println only after //create an array and convert to list Character[] ray = {'p','w','n'}; List l = Arrays.asList(ray); System.out.println("List is : "); output(l); //method to be created ///reverse and printout the list Collections.reverse(l); //reverse reverse everyting System.out.println("After reverse: "); output(l); //copy of a list //create a new array and a new list Character[] newRAY = new Character[l.size()]; List lcopy = Arrays.asList(newRAY); Collections.copy(lcopy, l); System.out.println("copy of list: "); output(lcopy); //fill collections with crap Collections.fill(l, 'x'); System.out.println("After filling the list! "); } private static void output(List lcopy) { // TODO Auto-generated method stub } public static void printMe(List l) { for (String b : l) { System.out.printf("%s ", b); } } public static void removeStuff(List l, int from, int to) { l.subList(from, to).clear(); } public static void reverseMe(List l) { ListIterator bob = l.listIterator(l.size()); System.out.println(); while (bob.hasPrevious()) { System.out.printf("%s ", bob.previous()); } } public static void editlist(Collection l1, Collection l2) { Iterator it = l1.iterator(); while (it.hasNext()) { if (l2.contains(it.next())) it.remove(); } } /** * recursion /* a method that calls itself /* ex factorial */ public static long fact(long n) { if (n <= 1) // base case return 1; else return n * fact(n - 1); } }