Examples on Arrays and ForEach Loop.

In this article,Examples on Arrays and ForEach Loop are explained in detail with examples. we will solve some examples related to the arrays and learn about Foreach Loop.
Ex
//programs related to Examples on Arrays and ForEach Loop--the sum of elements in an array.
class ArraySum
{
public static void main(String args[])
{
int a[]={11,22,33,44,55};
int i,sum=0;
for(i=0;i<5;i++)
sum=sum+a[i];
System.out.println("The Total is: "+sum);
}
}
Output
Array Example-Javaform
                           To get the code file use me.
Ex
//program to Examples on Arrays and ForEach Loop--print reverse order of an array using java.util package.
import java.util.*;
class ArrayInput
{
public static void main(String args[])
{
int a[],i;
a=new int[5];
Scanner scan=new Scanner(System.in);
System.out.println("Enter any 5 values: ");
for(i=0;i<5;i++)
a[i]=scan.nextInt();
System.out.println("Given values in reverse order: ");
for(i=4;i>=0;i--)
System.out.println(a[i]+"\t");
}
}
Output
Array Example-javaform
                            To get code file use me.

Advantages of Arrays.
  • Accessing elements is simple,fast and easy.
  • Speed in execution of program(High Performance)
For Each Loops
This loop was introduced in Jdk 1.5. It is used to execute a block of statements iteratively based on the number of elements in an array or collection.  (From the topic Examples on Arrays and ForEach Loop)
Syntax
for(datatype variable:array/collection)
{
-----
body;
-----
}
Ex
int a[]={11,22,33,44,55}; //5 elements
for(int i:a) //Loop executes 5 times
{
System.out.println("I value is: "+i);
}
Here, each value of array is assigned to variable 'i' and 'i' can be used in the body of the loop.
(From the topic Examples on Arrays and ForEach Loop)

Ex
\\ program related to Examples on Arrays and ForEach Loop--Foreach Loop
class ForEachLoop
{
public static void main(String args[])
{
int a[]={11,22,33,44,55,66,77,88};
int sum=0;
for(int i:a)
{
System.out.println("value: "+i);
sum=sum+i;
}
System.out.println("Sum is: "+sum);
}
}
Output
For each Loop-Javaform

Varargs
This concept was introduced  in JDK 1.5,it is known as variable length arguments list.
It is used in methods as the input parameter.
Ex
To perform sum of integers(two,three,four,-----etc..,)
we use the following methods.
Ex
int sum(int a, int b)               //(From the topic Examples on Arrays and ForEach Loop)
{
----
}
int sum(int a, int b,int c)
{
----
}
int sum(int a,int b,int c,int d)
{
----
}
Like this, we have to take many sum methods based on sum type of input parameters.
To overcome this, we use var-args, it is represented with "...variable name" as a single parameter to the method.
Ex
int sum(int ...a)
{
----
---
}
Here, this varargs,works like a array with dynamic size.Which means we can pass zero or more arguments to call this method.
how we use:
sum(10,20);
sum(11,22,33);
sum(65,98,59,76);       (From the topic Examples on Arrays and ForEach Loop)
Note
While using varargs make sure such parameters is used last parameters of the method but not first or second or middle.
case (i)
void m1(String s, int...a)
m1("hi");---It is Accepted
m1("hai",10);
m1("hello",10,20);
all these types are accepted as all these cases have first parameter in it.
case(ii)
void m2(int...a,String s)
m2( ,"hi");----error
m2(10,"hi")----accepted because it consist of first parameter.

Ex
//program related to Examples on Arrays and ForEach Loop--varargs
class VaraiableArgs //varargs JDK 1.5
{
public static void method1(int x,String...p) //varargs works
{
System.out.println("X value is: "+x);
for(int i-0;i<p.length;i++)
{
System.out.println("String is: "+p[i]);
}
}
public static void main(String args[])
{
method1(10);
String a[]={"welcome","Great","hi","hello","best"};
method1(30,a);
}
}
Output
Varargs output-javaform

Ex
//program related to Examples on Arrays and ForEach Loop--varargs
class VaraiableArgs //varargs JDK 1.5
{
public static void method1(int x,String...p) //varargs works
{
System.out.println("X value is: "+x);
for(int i-0;i<p.length;i++)
{
System.out.println("String is: "+p[i]);
}
}
public static void main(String args[])
{
method1(20,"hello");
String a[]={"welcome","Great","hi","hello","best"};
method1(30,a);
}
}
Output
Variable argument-javaform

Ex
//program related to Examples on Arrays and ForEach Loop--varargs.
class VariableArgs
{
public static void method1(int x,String...p)
{
System.out.println("X value is: "+x);
for(int i=0;i<p.length;i++)
{
System.out.println("String is: "+p[i]);
}
}
public static void main(String args[])
{
method1(25,"hello","Welcome");
String a[]={"welcome","Great","hi","hello","best"};
method1(30,a);
}
}
Output
Variable Args-javaform

Continue to the next topic Possible array declarations and 2D Arrays.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required