1. RemoveAll in ArrayList
package abc;
import java.util.ArrayList;
import java.util.Arrays;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] args) {
ArrayList<String> alist = new ArrayList<>();
String[] names = {"Ram", "Hari","Sita","Gopi","Raju"};
int lengthArray = names.length;
for(int i=0;i<lengthArray;i++) {
alist.add(names[i]);
}
//alist : [Ram, Hari, Sita, Gopi, Raju]
ArrayList<String> blist = new ArrayList<>();
for(int i=0;i<lengthArray-1;i++) {
blist.add(names[i]);
}
//blist: [Ram, Hari, Sita, Gopi]
ArrayList<String> clist = new ArrayList<>();
for(int i=1;i<lengthArray;i++) {
clist.add(names[i]);
}
//clist = [Hari, Sita, Gopi, Raju]
ArrayList<String> dlist = new ArrayList<>();
dlist.add(names[0]);
dlist.add(names[1]);
dlist.add(names[3]);
dlist.add(names[4]);
//dlist = [Ram, Hari, Gopi, Raju]
System.out.println("array = "+Arrays.toString(names));
System.out.println("alist = "+alist);
System.out.println("blist = "+blist);
System.out.println("clist = "+clist);
System.out.println("dlist = "+dlist);
// ArrayList<String> elist = new ArrayList<>();
// ArrayList<String> flist = new ArrayList<>();
// ArrayList<String> glist = new ArrayList<>();
//
// //elist = this does not work this way
alist.removeAll(blist);
//flist = this does not work this way
// alist.removeAll(clist);
// //glist = this does not work this way
// alist.removeAll(dlist);
System.out.println("alist after remove = "+alist);
}
}
Output:
array = [Ram, Hari, Sita, Gopi, Raju]
alist = [Ram, Hari, Sita, Gopi, Raju]
blist = [Ram, Hari, Sita, Gopi]
clist = [Hari, Sita, Gopi, Raju]
dlist = [Ram, Hari, Gopi, Raju]
alist after remove = [Raju]
When “alist.removeAll(clist);” is used:
Output:
alist after remove = [Ram]
When “alist.removeAll(dlist);” is used:
Output:
alist after remove = [Sita]
2. Enter string/integer, SPLIT, Enter words one by one
package abc;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//enter a String and split it
Scanner scnr = new Scanner(System.in);
System.out.println("Enter the String");
String str = scnr.nextLine();
System.out.println("The entered string = "+str);
Output:
The entered string = There are 7 days in a week.
//Split the given string
System.out.println("*****Spliting String******");
String[] splitString = str.split(" ");
System.out.println("Array of the entered String is :"+Arrays.toString(splitString));
Output:
Array of the entered String is :[There, are, 7, days, in, a, week.]
int lengthStr = splitString.length ;
for(int i=0;i<lengthStr;i++) {
System.out.println("word"+(i+1)+" = "+splitString[i]);
}
Output:
*****Spliting String******
word1 = There
word2 = are
word3 = 7
word4 = days
word5 = in
word6 = a
word7 = week.
//enter an integer
Scanner scnr1 = new Scanner(System.in);
System.out.println("Enter the integer");
int num = scnr1.nextInt();
System.out.println("The integer = "+num);
Output:
The integer = 78
}
}
3. Enter string/words in loop or sequence
package abc;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//enter string in loop
String enteredString = " ";
Scanner scnr2 = new Scanner(System.in);
for(int i=0;i<7;i++) {
System.out.println("Enter word"+(i+1));
String str2 = scnr.nextLine();
enteredString = enteredString.concat(" ").concat(str2);
}
System.out.println("Entered String = "+enteredString);
enteredString = enteredString.trim();
System.out.println("Entered String = "+enteredString); //trimmed
}
}
Output:
Enter word1
This
Enter word2
is
Enter word3
a
Enter word4
beautiful
Enter word5
world,
Enter word6
Yes
Enter word7
No
Entered String = This is a beautiful world, Yes No
Entered String = This is a beautiful world, Yes No
No comments:
Post a Comment