Array Length.zip

Java Arrays

Java Arrays

8,603

Arrays are used to store multiple values in a single variable,instead of declaring separate variables for each value.To declare an array,define the variable type with square brackets:We have now declared a variable that holds an array of strings.To insert ...





Java Arrays

JK 2006 
Created at
Updated at  
8,604 0 0

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets:

String[] cars;

We have now declared a variable that holds an array of strings. To insert values to it, you can place the values in a comma-separated list, inside curly braces:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of integers, you could write:

int[] myNum = {10, 20, 30, 40};

Access the Elements of an Array

You can access an array element by referring to the index number.

This statement accesses the value of the first element in cars:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo

Change an Array Element

To change the value of a specific element, refer to the index number:

Example 1)

cars[0] = "Opel";

Example 2)

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo

Array Length

To find out how many elements an array has, use the length property:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4

Loop Through an Array

You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.

The following example outputs all elements in the cars array:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
  System.out.println(cars[i]);
}

Loop Through an Array with For-Each

There is also a "for-each" loop, which is used exclusively to loop through elements in arrays:

for (type variable : arrayname) {
  ...
}

The following example outputs all elements in the cars array, using a "for-each" loop:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
  System.out.println(i);
}

The example above can be read like this: for each String element (called i - as in index) in cars, print out the value of i.

If you compare the for loop and for-each loop, you will see that the for-each method is easier to write, it does not require a counter (using the length property), and it is more readable.


Multidimensional Arrays

A multidimensional array is an array of arrays.

Multidimensional arrays are useful when you want to store data as a tabular form, like a table with rows and columns.

To create a two-dimensional array, add each array within its own set of curly braces:

int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };

myNumbers is now an array with two arrays as its elements.


Access Elements

To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array. This example accesses the third element (2) in the second array (1) of myNumbers:

int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(myNumbers[1][2]); // Outputs 7

Change Element Values

You can also change the value of an element:

int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers[1][2] = 9;
System.out.println(myNumbers[1][2]); // Outputs 9 instead of 7

Loop Through a Multi-Dimensional Array

We can also use a for loop inside another for loop to get the elements of a two-dimensional array (we still have to point to the two indexes):

public class Main {
  public static void main(String[] args) {
    int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
    for (int i = 0; i < myNumbers.length; ++i) {
      for(int j = 0; j < myNumbers[i].length; ++j) {
        System.out.println(myNumbers[i][j]);
      }
    }
  }
}

 

Below YouTube content is also helpful for better understanding:

Tags Array Array Element Array Length Java Array Multi-Dimensional Array Facebook X
Comments 0
Recently updated
  1. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    93
  2. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    35
  3. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    164
  4. The Evolution and Production Reality of Agentic AI
    161
  5. How to Activate or Waive Your UIUC Student Health Insurance
    237
  6. Complete Guide to Building a Machine Learning Model
    281
  7. My life cuts at Las Vegas during Thanksgiving day holiday
    7,362
  8. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    307
  9. Harness vs. OpenClaw: Two Very Different "Agents"
    889
  10. Clean Python Environments: The Power of venv vs. Docker
    758
  11. What is Docker? Why is Docker also useful in a development environment?
    578
  12. UIUC 2026-2027 Academic Calendar
    1,453
  13. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    759
  14. Open-Source LLMs: The AI Revolution
    704
  15. Resume 2.0: Leveling Up for My First Software Gig
    2,057
  16. Not everyone will understand what this man just did
    1,712
  17. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,543
  18. Unpacking IU's Shopper
    703
  19. Jackie Chan's Police Story: The Action Masterpiece
    601
  20. The IVE Story: Identity, 'I AM' Charts, and Influence
    901
  21. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,145
  22. Open Databases for Sex Crime Occurrences in the U.S.
    673
  23. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,507
  24. My First Day at University of Illinois-Urvana Champaign
    1,154
  25. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,081
  26. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,153
  27. Sipping the Stars: My Starbucks Adventure
    9,567
  28. Exciting explore at Sequoia National Park
    7,613
  29. My Life Shot at Death Valley
    1,617
  30. Ip Man fights with Muay Thai Master
    897
  31. Mad Clown - Don't Die
    971
  32. How to get Student Enrollment and Degree Verification at UIUC
    4,639
  33. LAX Thanksgiving Rush: A Joyful Reunion
    893
  34. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,075
  35. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    938
  36. 10CM - To Reach You (너에게 닿기를)
    1,122
  37. Feeling weak? Transform yourself at the UIUC ARC!
    1,544
  38. BOYNEXTDOOR - If I Say I Love You
    1,152
  39. The Future of Software Engineer - AI Engineering
    912
  40. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    876
  41. Lie - Legend song by BIGBANG
    7,786
  42. Why ROLLBACK is useful when you work with Google Gemini CLI?
    803
  43. Reimbursement after Vaccination at McKinley Health Center
    970
  44. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    936
  45. Common Questions from UIUC school life in terms of CS Program
    1,068
  46. UIUC Immunization Compliance
    1,164
  47. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,060
  48. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,058
  49. Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)
    841
  50. Common methods to improve coding skills
    956