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

Array Java

This document discusses arrays in Java. It covers how to declare and create arrays, initialize arrays with values, loop through arrays to print or sum elements, pass arrays to methods, return arrays from methods, and some examples of array problems to solve like summing array values or reversing an array.

Uploaded by

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

Array Java

This document discusses arrays in Java. It covers how to declare and create arrays, initialize arrays with values, loop through arrays to print or sum elements, pass arrays to methods, return arrays from methods, and some examples of array problems to solve like summing array values or reversing an array.

Uploaded by

isayashpbende26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Array in Java

Declaring Array Variables



Syntax
dataType[] arrayRefVar; // preferred way.
or dataType arrayRefVar[]; // works but not preferred
way.

• The following code snippets are examples of this


syntax −
• double[] myList; // preferred way.
• or double myList[]; // works but not preferred way.
Creating Arrays
Syntax
arrayRefVar = new dataType[arraySize];
The above statement does two things −
It creates an array using new dataType[arraySize].
It assigns the reference of the newly created array
to the variable arrayRefVar.

Declaring an array variable, creating an array, and assigning the reference of


the array to the variable can be combined in one statement, as shown
below −

dataType[] arrayRefVar = new dataType[arraySize];

Alternatively you can create arrays as follows −

dataType[] arrayRefVar = {value0, value1, ..., valuek};


Example
public class TestArray
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++)
{
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) Output

1.9 2.9 3.4 3.5


{
total += myList[i];
}
System.out.println("Total is " + total);

// Finding the largest element


Total is 11.7
double max = myList[0];
for (int i = 1; i < myList.length; i++) Max is 3.5
{
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max); } }
For each loop
Example
The following code displays all the elements in the array myList −
public class TestArray
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList)
{
System.out.println(element);
}
}
}

This will produce the following result −


Output
1.9 2.9 3.4 3.5
Passing Arrays to Methods
Example
public static void printArray(int[] array)
{
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
}

You can invoke it by passing an array. For example, the following


statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2

Example
printArray(new int[]{3, 1, 2, 6, 4, 2});
class Test
{
public static void main(String args[])
{
int arr[] = {3, 1, 2, 5, 4};

// passing array to method sum


sum(arr);

public static void sum(int[] arr)


{
// getting sum of array values
int sum = 0;

for (int i = 0; i < arr.length; i++)


sum+=arr[i];

System.out.println("sum of array values : " + sum);


}
}
Output :
sum of array values : 15
Returning an Array from a Method

Example
public static int[] reverse(int[] list)
{
int[] result = new int[list.length];
for (int i = 0, j = result.length - 1; i < list.length; i++, j--)
{
result[j] = list[i];
}
return result;
}
Assignment on array
1. Write a java program to sum values of an array
2. Write a java program to sum values of an array
for odd nos.
3. Write a java program to sum values of an array
for even nos.
4. Write a java program to calculate the sum and
average value of array elements.
5. Write a java program to reverse an array of
integer values
import java.util.*; class Main{int id;String
name;void setData(int p, String q){ id=p;
name=q;}void display()
{ System.out.println(id+" "+name);}public
static void main(String args[]){ int roll;String
n;Main s1[]= new Main[3];Scanner sc = new
Scanner(System.in); for(int i=0;i<3;i++)
{ s1[i]=new Main();
System.out.println("entre roll no"); roll =
sc.nextInt(); System.out.println("entre
name"); n = sc.nextLine();
s1[i].setData(roll,n);} for(int i=0;i<3;i++)
{ s1[i].display();}}}

You might also like