Java arrays cheat sheet
Array Cheatsheet
- Arrays are always created in HEAP.
- Arrays can contain both Primitives and Objects.
- Size of Array can not be included in Declaration.
- Declaration doesn’t create array object in heap.
Array Cheatsheet
Do – While loop is same as While loop except in this case loop is confirmed to execute once(Means in any case statements in Do-While block will execute atleast once).
In previous article we have covered for loop here we will discuss about the other type of looping statement WHILE. WHILE statement is useful when you want to execute something till a particular condition is true.
In Previous post we have discussed the Condition Statement. This Article (And next two Articles) will discuss about the Looping statement in Java Language.
Looping statement can be of 3 types.
Here we will learn to create constructors with arguments and how to use those constructors.
Here we will learn to create no argument(no-arg) constructor in Java.
Here in this article we will discuss the difference between equals() method and “==” operator.
package com.list;
import java.util.ArrayList;
import java.util.List;
/*
* Here we will learn to convert List to Arrays.
*/
public class ListToArrayType {
/*
* Note*: Here in this code Autoboxing is done also generics is getting used. Because of generics
* Casting is not required.
*/
public static void main(String[] args) {
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
System.out.println("Values in List-");
for(int str: list)
{
System.out.println(str);
}
System.out.println("Converting list in Arrays");
Integer[] strArr = list.toArray(new Integer[0]);
System.out.println("Values in String arrays");
for(int str : strArr)
System.out.println(str);
}
}
package com.string;
import java.util.Scanner;
/*
* Here we will learn to split the string based on whitespace.
*/
public class StringSplitWhiteSpace {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the string to split.");
String strToSplit = scanner.nextLine();
/*
* Whitespace Character contains
* 1- \t
* 2- \n
* 3- \f
* 4- \r
*
* In combination whitespace can be represented by \s
*
* Note* : Here s is SMALL.
*/
System.out.println("Complete String : " + strToSplit);
System.out.println("String after split");
String strArr[] = strToSplit.split("\\s");
for (String str : strArr)
System.out.println(str);
}
}
Here we will learn about the List interface and different implementation of it in details. List Interface which is in java.util package is a subtype of java.util.Collection Interface.
List listA = new ArrayList();
List listB = new LinkedList();
List listC = new Vector();
List listD = new Stack();
List listA = new ArrayList();
listA.add("element 1");
listA.add("element 2");
listA.add("element 3");
listA.add(0, "element 0");