0% found this document useful (0 votes)
4 views

Array

Uploaded by

Bipin Bhadra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array

Uploaded by

Bipin Bhadra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

For-each Loop for Java Array:

We can also print the Java array using for-each loop. The Java for-each loop prints the array elements one by
one. It holds an array element in a variable, then executes the body of the loop.
Syntax:
for(data_type variable:array){
//body of the loop
}
Program:

class Testarray1{
public static void main(String args[]){
int arr[]={1,2,3,4,5};
//printing array using for-each loop
for(int i:arr)
System.out.println(i);
}
}
Java Methods:

• A method is a block of code which only runs when it is called.

• You can pass data, known as parameters, into a method.

• Methods are used to perform certain actions, and they are also known as functions.

• Why use methods? To reuse code: define the code once, and use it many times.

Create a Method:

A method must be declared within a class. It is defined with the name of the method, followed by
parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can also
create your own methods to perform certain actions:

Example:
public class Main {
static void myMethod() {
// code to be executed
}
}

Example Explained:

• myMethod() is the name of the method.


• static means that the method belongs to the Main class and not an object of the Main class.
• void means that this method does not have a return value.

Call a Method:

To call a method in Java, write the method's name followed by two parentheses () and a semicolon;

In the following example, myMethod() is used to print a text (the action), when it is called:

Example:

Inside main, call the myMethod() method:

public class Main {

static void myMethod() {

System.out.println("I am studying MCA");

public static void main(String[] args) {

myMethod();

Example: A method can also be called multiple times.


public class Main {

static void myMethod() {

System.out.println("I just got executed!");

public static void main(String[] args) {

myMethod();

myMethod();

myMethod();

Passing Array to a Method in Java:

We can pass the java array to method so that we can reuse the same logic on any array.
Example:
class Testarray2{
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i])
min=arr[i];
System.out.println(min);
}
public static void main(String args[]){
int a[]={1,3,4,5};
min(a); //passing array to method
}
}

Anonymous Array in Java:


An array in java without any name is known as an anonymous array. It is an array just for creating and
using instantly. Using an anonymous array, we can pass an array with user values without the referenced
variable.
Properties of Anonymous Arrays:
• We can create an array without a name. Such types of nameless arrays are called anonymous
arrays.
• The main purpose of an anonymous array is just for instant use (just for one-time usage).
• An anonymous array is passed as an argument of a method.
Note: For Anonymous array creation, do not mention size in []. The number of values passing inside {} will
become the size.

Example:
//Java Program to demonstrate the way of passing an anonymous array to method.
class TestAnonymousArray{
//creating a method which receives an array as a parameter
static void printArray(int arr[]){
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}
public static void main(String args[]){
printArray(new int[]{10,20,30,40}); //passing anonymous array to method
}
}

You might also like