Java Arrays

JK JK 2026 
Created at
Updated at  
8,772 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
  1. Java Servlet Example
    7,135
  2. How do I replace content that based on the HTML UI Template
    7,166
  3. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,188
  4. Dataset of California Foodbanks
    7,189
  5. Java Tutorials associated with AP Computer Science A
    7,659
  6. Java Inner Classes
    7,077
  7. Java Polymorphism
    7,068
  8. Java Inheritance (Subclass and Superclass)
    7,192
  9. Java Packages
    7,190
  10. Java Abstract Classes and Methods
    7,771
  11. Java Classes and Objects
    7,110
  12. Java Recursion
    7,209
  13. Java Scope
    7,122
  14. Java Methods
    7,106
  15. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,908
  16. Java Switch Statements
    7,424
  17. Java Short Hand If...Else (Ternary Operator)
    7,601
  18. Java If ... Else
    7,417
  19. Java Math
    7,455
  20. Java Variables
    7,490
  21. Java Comments
    8,059
  22. The Print() Method
    7,174
  23. Java Syntax
    7,468
  24. Java Getting Started
    7,425
  25. What is Java?
    7,108
Recently updated
  1. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    123
  2. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    128
  3. Watch This Dazzling Dancer’s Mind-Blowing Moves
    158
  4. Michael Jackson's Billie Jean
    225
  5. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    204
  6. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    621
  7. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    321
  8. The Evolution and Production Reality of Agentic AI
    279
  9. How to Activate or Waive Your UIUC Student Health Insurance
    359
  10. Complete Guide to Building a Machine Learning Model
    389
  11. My life cuts at Las Vegas during Thanksgiving day holiday
    7,482
  12. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    416
  13. Harness vs. OpenClaw: Two Very Different "Agents"
    1,065
  14. Clean Python Environments: The Power of venv vs. Docker
    905
  15. What is Docker? Why is Docker also useful in a development environment?
    675
  16. UIUC 2026-2027 Academic Calendar
    1,709
  17. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    875
  18. Open-Source LLMs: The AI Revolution
    827
  19. Resume 2.0: Leveling Up for My First Software Gig
    2,413
  20. Not everyone will understand what this man just did
    2,014
  21. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,663
  22. Unpacking IU's Shopper
    834
  23. Jackie Chan's Police Story: The Action Masterpiece
    713
  24. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,022
  25. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,243
  26. Open Databases for Sex Crime Occurrences in the U.S.
    771
  27. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,823
  28. My First Day at University of Illinois-Urvana Champaign
    1,252
  29. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,217
  30. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,297
  31. Sipping the Stars: My Starbucks Adventure
    9,770
  32. Exciting explore at Sequoia National Park
    7,729
  33. My Life Shot at Death Valley
    1,793
  34. Ip Man fights with Muay Thai Master
    996
  35. Mad Clown - Don't Die
    1,094
  36. How to get Student Enrollment and Degree Verification at UIUC
    5,097
  37. LAX Thanksgiving Rush: A Joyful Reunion
    965
  38. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,191
  39. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,038
  40. 10CM - To Reach You (너에게 닿기를)
    1,210
  41. Feeling weak? Transform yourself at the UIUC ARC!
    1,621
  42. BOYNEXTDOOR - If I Say I Love You
    1,238
  43. The Future of Software Engineer - AI Engineering
    971
  44. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    970
  45. Lie - Legend song by BIGBANG
    7,877
  46. Why ROLLBACK is useful when you work with Google Gemini CLI?
    908
  47. Reimbursement after Vaccination at McKinley Health Center
    1,074
  48. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    1,015
  49. Common Questions from UIUC school life in terms of CS Program
    1,183
  50. UIUC Immunization Compliance
    1,271