Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class myClass {
- public static void main(String[] args){
- // 1. String methods
- System.out.println("****************************************");
- System.out.println("1. String Methods");
- String[] words = {"funk", "chunk", "furry", "bacon", "clark"};
- for(String word : words){
- if(word.startsWith("fu")){
- System.out.println(word + " ---> starts with 'fu'");
- }
- if(word.endsWith("nk")){
- System.out.println(word + " ---> ends with 'nk'");
- }
- if(word.endsWith("k")){
- System.out.println(word + " ---> ends with 'k'");
- }
- }
- System.out.println();
- // 2. More string methods - startsWith, endsWith, indexOf, replace, toUpperCase, toLowerCase, trim
- System.out.println("****************************************");
- System.out.println("2. More string methods");
- String s1 = "buckyrobertsbuckyroberts";
- System.out.println(s1 + " ---> Index of 'k' = " + s1.indexOf('k'));
- System.out.println(s1 + " ---> Index of 'k' after index 5 = " + s1.indexOf('k', 5));
- System.out.println(s1 + " ---> Index of 'w' = " + s1.indexOf('w'));
- System.out.println(s1 + " ---> Index of 'rob' = " + s1.indexOf("rob"));
- String a = "Bacon";
- String b = "Monster";
- String c = " Hello ";
- System.out.println(a.concat(b));
- System.out.println(a.replace('c', 'R'));
- System.out.println(b.replace("nst", "us"));
- System.out.println(c);
- System.out.println(c.trim());
- System.out.println();
- // 3. Recursion
- System.out.println("****************************************");
- System.out.println("3. Recursion");
- System.out.println("Factorial(5) = " + fact(5));
- System.out.println("Factorial(6) = " + fact(6));
- System.out.println();
- // 4. Collections - add, size, get
- System.out.println("****************************************");
- System.out.println("4. Collections");
- String[] things = {"eggs", "lasers", "hats", "pie"};
- List<String> list = new ArrayList<String>();
- for(String thing : things){
- list.add(thing);
- }
- System.out.println(list);
- String[] more = {"lasers", "hats"};
- List<String> list2 = new ArrayList<String>();
- for(String y : more){
- list2.add(y);
- }
- System.out.println(list2);
- for(int i=0; i<list.size(); i++){
- System.out.printf("%s ", list.get(i));
- }
- System.out.println();
- editlist(list, list2);
- System.out.println(list);
- } // END OF MAIN
- public static long fact(int n){
- if(n <= 1)
- return 1;
- else
- return n * fact(n-1);
- }
- public static void editlist(Collection<String> list1, Collection<String> list2){
- Iterator<String> it = list1.iterator();
- while(it.hasNext()){
- if(list2.contains(it.next())){
- it.remove();
- }
- }
- }
- } // END OF CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement