MainMenu

Home Java Overview Maven Tutorials

Sunday, 15 January 2017

Boolean Operator, Array & ArrayList In java with example






For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


Core java Program of Boolean Operator


For Video : CLICK HERE




Boolean operator is used to get the result in TRUE/FALSE form based on certain conditions.


public class Test {
public static void main(String[] args)
{
boolean b,c;
b = (5>1);
c = (1>5);
System.out.println("Your result is :" +b);
System.out.println("Your result is :" +c);
}
}
Output :
Your result is : True
Your result is : False

Also Read :Introduction of Java, Why java is platform independent

Array In java

Array is a collection of similar type of elements that have contiguous memory location.
Java array is an object that have elements of similar data type.
It is also known as static data structure because size of an array must be specified at the time of its declaration.

New Operator:

new operator is used to initialize an array.

Example :
int[] arr = new int[5]; //5 is the size of array.

Types of array in Java


In java there is only twi types of array :
1. Single Dimensional Array.
2. Multidimensional Array.

Array Example:


public class hello
{
public static void main(String[] args)
{
int a[] = new int [10];
for (int i=0; i<a.length; i++)
{
a[i]=i;
}
for (int element:a)
{
System.out.println(element);
}
}
}
output : 0
1
2
3
4
Note : array can also be declared as “ int a[] = {2, 4,67,43};”
Array Deceleration Example :-

int Array[];
Array = new int[10];
OR

int[] Array = new int[10];
OR

int[] Array = new int[]{2,5,7,9,11};
String Array
String[] mystringArray = new String[3];
OR

String[] mystringarray = {"b", "a", "c"};
OR

String[] mystringarray = new String[]{"b", "a", "c"};



ArrayList in java with example


Java provide a library called util and this have keyword ArrayList and this help to maintain and dynamic use of Array.
These arraylist are dynamically created and used. To use ArrayList you have to import ArrayList.
import java.util.ArrayList will add the class ArrayList in the program.
Syntax :
Arraylist Array_Name = new Arraylist<>(initial size)
example :
ArrayList mylist = new ArrayList(5)

Example of ArrayList In java :

In the below example we have created a dynamic size array and printed its elements.
also we changed the value of Array element which is at intex 3.
package csc;
import java.util.ArrayList;
public class C2
{
public static void main(String[] args)
{
ArrayList mylist = new ArrayList(5);
mylist.add(110);
mylist.add(200);
mylist.add(307);
mylist.add(307);
mylist.add(357);
mylist.add(27);
for (Integer x : mylist)
{
System.out.println(x);
}
System.out.println("Array size :" +mylist.size());
mylist.set(3, 204);
for (Integer x : mylist)
{
System.out.println(x);
}
System.out.println("Array size :" +mylist.size());
mylist.clear();
System.out.println("Array size :" +mylist.size());
}
}
Output :
110
200
307
307
357
27
Array size :6
110
200
307
204
357
27
Array size :6
Array size :0

Tags:Example of Boolean operator in Java, Array in java with example, Java ArrayList Class

No comments:

Post a Comment