As we all know, an array is a set or a collection of items. What is the first step that comes to your mind when we talk about creating arrays? Did you say "Length!!" .Yes, usually we create an array initializing its length and then input the elements in it. Have you ever thought of creating an array first without declaring its length? Well, for that let's create a dynamic array.
Contents:
Step 1: Create a Java Class
Step2: Define a Dynamic array
Step3: Add the elements to the array and find its length
Step4: Define a method to find the largest number
Step5: Final program
Step 1: Create a Java Class
Open Eclipse IDE and create a new Java class named Arrays.
Step 2: Define a Dynamic array
To create dynamic arrays in JAVA we use ArrayList. ArrayList is a part of collection framework and is present in java.util package.
ArrayList<Integer> array=new ArrayList<Integer>();
So, let's use the same in our program.
package com.training.ninja;
import java.util.ArrayList;
import java.util.Scanner;
public class Arrays {
public static void main(String[] args) {
ArrayList<Integer> array=new ArrayList<Integer>();
Scanner input=new Scanner(System.in);
int i=input.nextInt();
}
}//End of class
Step 3: Add the elements to the array and find its length
Now let us write a program to get the list of integers and store it in an array and to find the maximum number in that array. As we are not sure of how many items to add, let us just keep asking the user if he wants to input more items( 'y' for yes ).
package com.training.ninja;
import java.util.ArrayList;
import java.util.Scanner;
public class Arrays
{
public static void main(String[] args)
{
ArrayList<Integer> array=new ArrayList<Integer>();//Dynamic array
Scanner input=new Scanner(System.in);
System.out.println("Press 'y' to add a number to the array:");
while(true) {
char c=input.next().charAt(0);
if (c=='y')
{
System.out.println("Enter a number:");
int i=input.nextInt();
array.add(i);
System.out.println("Press 'y' to continue :");
}//End of if
else
{
break;
}//End of else
}//End of while
int len=array.size();
System.out.println("The array is:" +array+ "and its length is:" + len);
}
}
Yes!! You have successfully created an array and found its length. Next, let's see how to find the maximum element in the array.
Sample output:
Step 4: Define a method to find the largest number
Create an object for the class and call the function from the main method.
Arrays m = new Arrays();
Let us create a method/function to return the maximum element.
public int biggest_num(ArrayList<Integer> a, int l)
{
System.out.println("Find the maximum element in the array");
int max=0;
for( int i=0;i<l;i++)
{
if ((Integer) a.get(i)>max)
{
max=(Integer) a.get(i);
}//End of if
}//End of for
return max;
}
Note: The difference between an array and ArrayList when comparing an element with the integer it's (Integer) a.get(i).
Step 5: Final program
Our final program is ready now. Let's have a look at the code snippet and the output of the program.
package com.training.ninja;
import java.util.ArrayList;
import java.util.Scanner;
public class Arrays
{
public int biggest_num(ArrayList<Integer> a, int l)
{
System.out.println("Find the maximum element in the array");
int max=0;
for( int i=0;i<l;i++)
{
if ((Integer) a.get(i)>max)
{
max=(Integer) a.get(i);
}//End of if
}//End of for
return max;
}
public static void main(String[] args)
{
ArrayList<Integer> array=new ArrayList<Integer>();//Dynamic array
Scanner input=new Scanner(System.in);
System.out.println("Press 'y' to add a number to the array:");
while(true) {
char c=input.next().charAt(0);
if (c=='y')
{
System.out.println("Enter a number:");
int i=input.nextInt();
array.add(i);
System.out.println("Press 'y' to continue :");
}
else
{
break;
}
}
int len=array.size();
System.out.println("The array is:" +array+ "and its length is:" + len);
Arrays m = new Arrays();
System.out.println("The largest number of
the array is:"+ m.biggest_num(array,len));
}
}
Sample Output:
Hope this program will be helpful for Java beginners.
Happy coding!!
Nice 👍