For Enquiry: 93450 45466

Length attribute in Java: Finding the size of an array


You might already know from the previous blogs that an array is a list or collection of homogeneous elements or a collection of elements of the same data type, So in this tutorial, we will look into how to find the length of an array and few useful practice programs for you where you will need to use the length of the array.

So let us with this table of contentโ€ฆ
How to find the length of an array in java using the length field
Searching for a value in array using length field in java
Searching for the minimum value from the array using the length field in java
Searching for the maximum value from the array using the length field in java
Let us now jump into finding the length of an array using the length field in java.

How to find the length of an array in java using the length field

The length field or attribute in java returns the size or the number of elements present in the array, irrespective of the index of the last element in the array since the indexing starts at 0.

Let me show you an example

import java.util.*;
// Example program for finding the length of an array
public class Main {
public static void main(String[] args) {
String[] strArr = { "Get", "The", "Best", "Java", "Training", "At", "FITA", "Academy" };
int arrLen = strArr.length; // array length field
System.out.println("length of the array strArr: " + arrLen);
}
}
Output for the above program will be

length of the array strArr: 8
You might get confused with the length method. The length variable works on arrays to return size such as an array.length whereas the length() method works with strings to return the size of the string. Let me clear with an example

import java.io.*;
 
// Example
class Main {
// driver method
public static void main(String args[]) {
String strArr[] = { "Get", "The", "Best", "Java", "Training", "At", "FITA", "Academy" };
System.out.println("Length of string array strArr" + strArr.length);
String str = "Get The Best Java Training At FITA Academy";
System.out.println("Length of string str" + str.length());
}
}
Output for the above program will be

Length of string array strArr: 8
Length of string str: 42

Check out this Complete Online Java Course by FITA. FITA provides a complete Java course including core java and advanced java J2EE, and SOA training, where you will be building real-time applications using Servlets, Hibernate Framework, and Spring with Aspect-Oriented Programming (AOP) architecture, Struts through JDBC bundled with, placement support, and certification at an affordable price with an active placement cell, by expert software developers with over 10 years of working experience to make you an industry required certified java developer.

Using the length field on uninitialized arrays would throw you a compiler error and if you try to find the length of a null array it will throw a java.lang.NullPointerException. To avoid this, let us modify our program to find the length of the array without errors.

import java.io.*;
 
// Example program for finding the length of an array
public class Main {
private static void arrayLength(String[] strarr) {
if (strarr == null) {
System.out.println("Length cannot be determined for the array, "+strarr);
} else {
int arrLen = strarr.length;
System.out.println("The length of the array is: " + arrLen);
}
}
 
public static void main(String[] args) {
String[] myArr_1 = { "F","I","T","A", "A","c","a","d","e","m","y" };
String[] myArr_2 = { "Java", "T","r","a","i","n","i","n","g" };
String[] myArr_3 = { "123", "1", "2", "3", "5432" };
String[] myArr_4 = { "Java","Training" };
arrayLength(null);
arrayLength(myArr_1);
arrayLength(myArr_2);
arrayLength(myArr_3);
arrayLength(myArr_4);
}
}
  Output for the above program will be

Length cannot be determined for the array, null
The length of the array is: 11
The length of the array is: 9
The length of the array is: 5
The length of the array is: 2

After learning how to find the length of an array in Java, let us now learn how to search for a value in an array using the length field in java.

Searching for a value in array using length field in java

For searching a specific value in the array, we will loop through all the elements of the array, and compare each value with the specific value, and break out of the loop if the value is found else, print that the value is not found in the array.

We will need to have the length of the array to use for loop for determining how many times we will need to loop.

Here is a program for it.

import java.io.*;
 
// searching for a specific value using length of the array, and a for loop
public class Main {
private static boolean Search(String[] strArr, String input) {
if (strArr != null) { // check if array is nul
int arrLen = strArr.length
for (int i = 0; i <= arrLen - 1; i++) 
String value = strArr[i]
if (value.equals(input)) { // comparing with each elemen
return true;
 
}
}
}
return false;
 
}
 
public static void main(String[] args) {
String StringArray[] = { "Get", "The", "Best", "Java", "Training", "At", "FITA", "Academy" };
String searchfor[] = { "Worst", "FITA", "BEST","java","Java","Get" };
for (int i = 0; i <= searchfor.length -1; i++) {
boolean x=Search(StringArray, searchfor[i])
if (x) {
System.out.print("found your string "+searchfor[i]+" in my array");
} else {
System.out.print("Cannot find your "+searchfor[i]+" string in my array");
}
System.out.println();
}
}
}
Output for the above program

Cannot find your Worst string in my array
found your string FITA in my array
Cannot find your BEST string in my array
Cannot find your java string in my array
found your string Java in my array
found your string Get in my array

After learning how to search for a value from an array in Java, let us now learn how to search for the least value in an array using the length field in java.

Searching for the minimum value from the array using the length field in java

Let us write a program for finding the minimum value of elements in the array, by comparing each value with the other, and using the length of the array and a for loop.


import java.io.*;
// example program for searching for the minimum value from the elements of an array.
public class Main {
private static int minimum(int[] myArr) {
int minVal = myArr[0];
int arrLen = myArr.length;
for (int i = 1; i <= arrLen - 1; i++) {
int element = myArr[i];
if (element < minVal) {
minVal = element;
}
}
return minVal;
}
public static void main(String[] args) {
int[] inArr = { 20, 0, 0, 80, 12, 1, 4, 45, 78, 98 };
int inArrLen = inArr.length;
System.out.print("Elements in the array: ")
System.out.println();
for (int i = 1; i <= inArrLen - 1; i++) 
System.out.print(inArr[i]+" ");
}
System.out.println();
System.out.println("The Minimum element from the above array: " + minimum(inArr));
}
Output for the above program

Elements in the array:
0 0 80 12 1 4 45 78 98
 
The minimum element from the above array: 0

After learning how to search for the least value from an array in Java, let us now learn how to search for the highest value in an array using the length field in java.

Searching for the maximum value from the array using the length field in java

Let us write a program for finding the minimum value of elements in the array, by comparing each value with the other, and using the length of the array and a for loop.

import java.io.*;
 
// Example program for searching for the minimum value from the elements of an array.
public class Main {
private static int minimum(int[] myArr) {
int maxVal = myArr[0];
int arrLen = myArr.length;
for (int i = 1; i <= arrLen - 1; i++) {
int element = myArr[i];
if (element > maxVal) {
maxVal = element;
}
}
return maxVal;
}
 
public static void main(String[] args) {
int[] inArr = { 20, 0, 0, 80, 12, 1, 4, 45, 78, 98 };
System.out.println("The min element in the myArr: " + minimum(inArr));
}
}
Output for the above program

The min element in the myArr: 98

This was all about finding the length of arrays in java, finding the minimum and maximum value or an input value by the user from the array using the length field in java, along with few practice problems. To get in-depth knowledge of core Java and advanced java, J2EE and SOA training along with its various applications and real-time projects using Servlets, Spring with Aspect-Oriented Programming (AOP) architecture, Hibernate Framework, and Struts through JDBC you can enroll in Certified Java Training in Chennai or Certified Java Training in Bangalore by FITA or a virtual class for this course, at an affordable price, bundled with real-time projects, certification, support, and career guidance assistance and an active placement cell, to make you an industry required certified java developer.

FITAโ€™s courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.



Recent Post:

  • Trending Courses

    JAVA Training In Chennai Software Testing Training In Chennai Selenium Training In Chennai Python Training in Chennai Data Science Course In Chennai Digital Marketing Course In Chennai DevOps Training In Chennai German Classes In Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai UI UX Design course in Chennai Tally course in Chennai Full Stack Developer course in Chennai Salesforce Training in Chennai ReactJS Training in Chennai CCNA course in Chennai Ethical Hacking course in Chennai RPA Training In Chennai Cyber Security Course in Chennai IELTS Coaching in Chennai Graphic Design Courses in Chennai Spoken English Classes in Chennai Data Analytics Course in Chennai

    Spring Training in Chennai Struts Training in Chennai Web Designing Course In Chennai Android Training In Chennai AngularJS Training in Chennai Dot Net Training In Chennai C / C++ Training In Chennai Django Training in Chennai PHP Training In Chennai iOS Training In Chennai SEO Training In Chennai Oracle Training In Chennai Cloud Computing Training In Chennai Big Data Hadoop Training In Chennai UNIX Training In Chennai Core Java Training in Chennai Placement Training In Chennai Javascript Training in Chennai Hibernate Training in Chennai HTML5 Training in Chennai Photoshop Classes in Chennai Mobile Testing Training in Chennai QTP Training in Chennai LoadRunner Training in Chennai Drupal Training in Chennai Manual Testing Training in Chennai WordPress Training in Chennai SAS Training in Chennai Clinical SAS Training in Chennai Blue Prism Training in Chennai Machine Learning course in Chennai Microsoft Azure Training in Chennai Selenium with Python Training in Chennai UiPath Training in Chennai Microsoft Dynamics CRM Training in Chennai VMware Training in Chennai R Training in Chennai Automation Anywhere Training in Chennai GST Training in Chennai Spanish Classes in Chennai Japanese Classes in Chennai TOEFL Coaching in Chennai French Classes in Chennai Informatica Training in Chennai Informatica MDM Training in Chennai Big Data Analytics courses in Chennai Hadoop Admin Training in Chennai Blockchain Training in Chennai Ionic Training in Chennai IoT Training in Chennai Xamarin Training In Chennai Node JS Training In Chennai Content Writing Course in Chennai Advanced Excel Training In Chennai Corporate Training in Chennai Embedded Training In Chennai Linux Training In Chennai Oracle DBA Training In Chennai PEGA Training In Chennai Primavera Training In Chennai Tableau Training In Chennai Spark Training In Chennai Appium Training In Chennai Soft Skills Training In Chennai JMeter Training In Chennai Power BI Training In Chennai Social Media Marketing Courses In Chennai Talend Training in Chennai HR Courses in Chennai Google Cloud Training in Chennai SQL Training In Chennai CCNP Training in Chennai PMP Training in Chennai OET Coaching Centre in Chennai Business Analytics Course in Chennai NextJS Course in Chennai Vue JS Course in Chennai

  • Read More Read less
  • Are You Located in Any of these Areas

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!