Advertisement
makispaiktis

I1. Strings, Recursion, Collections

Jun 6th, 2022 (edited)
1,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class myClass {
  4.  
  5.     public static void main(String[] args){
  6.  
  7.         // 1. String methods
  8.         System.out.println("****************************************");
  9.         System.out.println("1. String Methods");
  10.         String[] words = {"funk", "chunk", "furry", "bacon", "clark"};
  11.         for(String word : words){
  12.             if(word.startsWith("fu")){
  13.                 System.out.println(word + " ---> starts with 'fu'");
  14.             }
  15.             if(word.endsWith("nk")){
  16.                 System.out.println(word + " ---> ends with 'nk'");
  17.             }
  18.             if(word.endsWith("k")){
  19.                 System.out.println(word + " ---> ends with 'k'");
  20.             }
  21.         }
  22.         System.out.println();
  23.  
  24.         // 2. More string methods - startsWith, endsWith, indexOf, replace, toUpperCase, toLowerCase, trim
  25.         System.out.println("****************************************");
  26.         System.out.println("2. More string methods");
  27.         String s1 = "buckyrobertsbuckyroberts";
  28.         System.out.println(s1 + " ---> Index of 'k' = " + s1.indexOf('k'));
  29.         System.out.println(s1 + " ---> Index of 'k' after index 5 = " + s1.indexOf('k', 5));
  30.         System.out.println(s1 + " ---> Index of 'w' = " + s1.indexOf('w'));
  31.         System.out.println(s1 + " ---> Index of 'rob' = " + s1.indexOf("rob"));
  32.         String a = "Bacon";
  33.         String b = "Monster";
  34.         String c = "      Hello   ";
  35.         System.out.println(a.concat(b));
  36.         System.out.println(a.replace('c', 'R'));
  37.         System.out.println(b.replace("nst", "us"));
  38.         System.out.println(c);
  39.         System.out.println(c.trim());
  40.         System.out.println();
  41.  
  42.         // 3. Recursion
  43.         System.out.println("****************************************");
  44.         System.out.println("3. Recursion");
  45.         System.out.println("Factorial(5) = " + fact(5));
  46.         System.out.println("Factorial(6) = " + fact(6));
  47.         System.out.println();
  48.  
  49.         // 4. Collections - add, size, get
  50.         System.out.println("****************************************");
  51.         System.out.println("4. Collections");
  52.         String[] things = {"eggs", "lasers", "hats", "pie"};
  53.         List<String> list = new ArrayList<String>();
  54.         for(String thing : things){
  55.             list.add(thing);
  56.         }
  57.         System.out.println(list);
  58.         String[] more = {"lasers", "hats"};
  59.         List<String> list2 = new ArrayList<String>();
  60.         for(String y : more){
  61.             list2.add(y);
  62.         }
  63.         System.out.println(list2);
  64.         for(int i=0; i<list.size(); i++){
  65.             System.out.printf("%s ", list.get(i));
  66.         }
  67.         System.out.println();
  68.         editlist(list, list2);
  69.         System.out.println(list);
  70.  
  71.  
  72.  
  73.     }   // END OF MAIN
  74.  
  75.     public static long fact(int n){
  76.         if(n <= 1)
  77.             return 1;
  78.         else
  79.             return n * fact(n-1);
  80.     }
  81.  
  82.     public static void editlist(Collection<String> list1, Collection<String> list2){
  83.         Iterator<String> it = list1.iterator();
  84.         while(it.hasNext()){
  85.             if(list2.contains(it.next())){
  86.                 it.remove();
  87.             }
  88.         }
  89.     }
  90.  
  91. }   // END OF CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement