Java Array.zip

Java Arrays

Java Arrays

8,660

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 2014 
Created at
Updated at  
8,660 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. Michael Jackson's Billie Jean
    72
  2. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    122
  3. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    341
  4. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    214
  5. The Evolution and Production Reality of Agentic AI
    200
  6. How to Activate or Waive Your UIUC Student Health Insurance
    283
  7. Complete Guide to Building a Machine Learning Model
    319
  8. My life cuts at Las Vegas during Thanksgiving day holiday
    7,397
  9. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    343
  10. Harness vs. OpenClaw: Two Very Different "Agents"
    962
  11. Clean Python Environments: The Power of venv vs. Docker
    814
  12. What is Docker? Why is Docker also useful in a development environment?
    610
  13. UIUC 2026-2027 Academic Calendar
    1,565
  14. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    802
  15. Open-Source LLMs: The AI Revolution
    748
  16. Resume 2.0: Leveling Up for My First Software Gig
    2,195
  17. Not everyone will understand what this man just did
    1,827
  18. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,580
  19. Unpacking IU's Shopper
    751
  20. Jackie Chan's Police Story: The Action Masterpiece
    650
  21. The IVE Story: Identity, 'I AM' Charts, and Influence
    950
  22. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,184
  23. Open Databases for Sex Crime Occurrences in the U.S.
    711
  24. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,609
  25. My First Day at University of Illinois-Urvana Champaign
    1,183
  26. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,119
  27. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,198
  28. Sipping the Stars: My Starbucks Adventure
    9,634
  29. Exciting explore at Sequoia National Park
    7,649
  30. My Life Shot at Death Valley
    1,692
  31. Ip Man fights with Muay Thai Master
    931
  32. Mad Clown - Don't Die
    1,009
  33. How to get Student Enrollment and Degree Verification at UIUC
    4,838
  34. LAX Thanksgiving Rush: A Joyful Reunion
    918
  35. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,117
  36. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    970
  37. 10CM - To Reach You (너에게 닿기를)
    1,152
  38. Feeling weak? Transform yourself at the UIUC ARC!
    1,569
  39. BOYNEXTDOOR - If I Say I Love You
    1,181
  40. The Future of Software Engineer - AI Engineering
    924
  41. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    900
  42. Lie - Legend song by BIGBANG
    7,809
  43. Why ROLLBACK is useful when you work with Google Gemini CLI?
    828
  44. Reimbursement after Vaccination at McKinley Health Center
    1,012
  45. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    963
  46. Common Questions from UIUC school life in terms of CS Program
    1,114
  47. UIUC Immunization Compliance
    1,206
  48. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,088
  49. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,090
  50. Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)
    864