
-
PDF for Module 7 Download from Here
StringBuilder Class:
public class Welcome {
public static void main(String[]
args) {
String webName = new String("Hadoop");
webName = webName+"Exam";
webName = webName+".com";
System.out.println(webName);
StringBuilder webName1 = new StringBuilder("Hadoop");
webName1 = webName1.append("Exam");
webName1 = webName1.append(".com");
System.out.println(webName1);
StringBuilder a = new
StringBuilder("Had");
StringBuilder b = a.append("oo");
b = b.append("p").append("Exam");
System.out.println("a=" + a);
System.out.println("b=" + b);
}
}
|
-
When we chained String method calls, the
result was a new String with
the answer. Chaining StringBuilder objects
doesn’t work this way. Instead, the StringBuilder
changes its own state and returns a reference to itself.
-
Size is the number of characters
currently in the sequence, and capacity is the number of characters the
sequence can currently hold. Since a String is immutable, the size and capacity
are the same. The number of characters appearing in the String is both the size
and capacity.
-
Do hands On your own with all the
StringBuilder methods (Which is quite similar to String methods)
equal method and == operator
public class Welcome {
public static void main(String[]
args) {
String name;
Welcome t1 = new Welcome();
Welcome t2 = new Welcome();
Welcome t3 = t1;
System.out.println(t1 == t1); // true
System.out.println(t1 == t2); // false
System.out.println(t1.equals(t2)); // false
}
}
|
Arrays in Java:
public class Welcome {
public static void main(String[]
args) {
String[] str = new String[]{"Hadoop","Exam",".com"}; //String
array
System.out.println("Length
of str array : "+ str.length);
String[] str1 = new String[5];
System.out.println("Default
values in String array : "+ str1[3]);
int[] values = new int[10];
System.out.println("Length
of int array : "+ values.length);
System.out.println("Default
values in int array : "+ values[5]);
}
}
|
public class Welcome {
public static void main(String[]
args) {
int[] values = {1,2,3}; //anonymous
array
//Below all 4 are equals
int[] val1;
int [] val2;
int val3[];
int val4 [];
//Another way
int val5[] , val6; //Here val5
is integer array and val6 is int variable
}
}
|
public class Welcome {
String welcomeMessage;
Welcome(String message){
welcomeMessage=message;
}
public static void main(String[]
args) {
Welcome[] welcomeArray = new Welcome[]{ new Welcome("HadoopExam"), new Welcome("QuickTechie") };
System.out.println(welcomeArray.length);
System.out.println(welcomeArray[0].welcomeMessage);
System.out.println(welcomeArray[1].welcomeMessage);
for(int i=0;i<welcomeArray.length;i++){
System.out.println("Welcome
to "+welcomeArray[i].welcomeMessage);
}
}
}
|
Sorting Array Elements
import
java.util.Arrays;
public class Welcome {
public static void main(String[]
args) {
int[] values = new int[]{6,3,7,1,4};
Arrays.sort(values);
for(int i=0;i<values.length;i++){
System.out.println(values[i]);
}
String[] str = new String[]{ "100" , "102" ,"10", "91"};
Arrays.sort(str);
for(int i=0;i<str.length;i++){
System.out.println(str[i]);
}
}
}
|
Element Searching: Java
also provides a convenient way to search - but only if the array is already
sorted.
Binary search rules:
Scenario
|
Result
|
Target element found in sorted
|
Index of match
|
Target element not found in
sorted array
|
Negative value showing one smaller than the
negative of index, where a match needs to be
inserted to preserve sorted order
|
Unsorted array
|
A surprise—this result isn’t predictable
|
import
java.util.Arrays;
public class Welcome {
public static void main(String[]
args) {
int[] numbers = {2,4,6,8};
System.out.println(Arrays.binarySearch(numbers, 2)); // 0
System.out.println(Arrays.binarySearch(numbers, 4)); // 1
System.out.println(Arrays.binarySearch(numbers, 1)); // -1
System.out.println(Arrays.binarySearch(numbers, 3)); // -2
System.out.println(Arrays.binarySearch(numbers, 9)); // -5
int[] numbers1 = new int[] {3,2,1};
System.out.println(Arrays.binarySearch(numbers1, 2));
System.out.println(Arrays.binarySearch(numbers1, 3));
}
}
|
Multidimensional Array: Array of Array
public class Welcome {
public static void main(String[]
args) {
int[][] intArray1; // 2D array
int intArray2 [][]; // 2D array
int[] intArray3[]; // 2D array
int[] intArray4 [], space [][]; // a 2D AND
a 3D array
int[][] value1 = new int[][]{{1,2,3} ,
{4,5,6} , {7,8}};
for(int i=0;i<value1.length ; i++){
for(int j=0;j<value1[i].length ; j++){
System.out.println(value1[i][j]);
}
}
//Another way to declare
array
int [][] values = new int[4][];
values[0] = new int[5];
values[1] = new int[3];
}
}
|
|
 Updating...
Ċ Training4Exam Info, Nov 17, 2016, 11:40 AM
|