Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Edwin Torres
Edwin Torres

Posted on

     

Basic Arrays in Java

Thearray is one of the fundamental and most useful concepts in programming. Most programming languages let programmers create and use arrays. Java is one of them.

An array is a group of variables that have the same name and data type. To access one of the variables, use the array name and the integer index of the variable. In Java, array indexes start with0. Arrays have a fixed length. Java implements arrays as objects.

To declare an array namedarr that can hold integer values:

int[]arr;
Enter fullscreen modeExit fullscreen mode

In the code above,arr is the variable name.int[] is the type, which is an array of integer values. This code only declares thearr variable. Here is how to allocate an array object with5 integer elements:

int[]arr=newint[5];
Enter fullscreen modeExit fullscreen mode

In this code,new is a Java keyword that allocates memory for an object andint[5] specifies an array of5 integer values. Since the default value for Javaint variables is0, the array contains five0 values in positions0-4.

Here is another way to allocate the same array:

int[]arr=newint[]{0,0,0,0,0};
Enter fullscreen modeExit fullscreen mode

The array object has alength property to indicate the size of the array:

intlen=arr.length;
Enter fullscreen modeExit fullscreen mode

In this array example,arr.length would return5.

To access an individual element of an array, supply an index to the array variable. The index is in square brackets[ ] after the array name:

arr[0]= 100;arr[1]= 200;arr[2]= 300;arr[3]= 400;arr[4]= 500;System.out.println( arr[3]); // 400
Enter fullscreen modeExit fullscreen mode

The first five lines assign the values100,200,300,400, and500 to array positions0,1,2,3, and4 respectively. Note that array elements are like integer variables, except with integer indexes.

The last line outputs the array element at position3:400. As you can see, an array element can be wherever an integer value is allowed.

Since the array indexes form a sequence of integers, arrays work well with loops. For an array of length5, it has the indexes0-4. Here is an example that uses afor loop to output each element of our array:

for(inti=0;i<arr.length;i++){System.out.println(arr[i]);}
Enter fullscreen modeExit fullscreen mode

And here is the output:

100200300400500
Enter fullscreen modeExit fullscreen mode

This example uses the loop variablei as the index of the array. The loop repeats its statement five times, once for each index of the array.

What if we want to sum all the elements in the array? It's easy. Use a loop:

intsum=0;for(inti=0;i<arr.length;i++){sum=sum+arr[i];}System.out.println(sum);// 1500
Enter fullscreen modeExit fullscreen mode

Here is the complete program:

publicclassExample{publicstaticvoidmain(String[]args)throwsException{int[]arr=newint[]{0,0,0,0,0};arr[0]=100;arr[1]=200;arr[2]=300;arr[3]=400;arr[4]=500;intsum=0;for(inti=0;i<arr.length;i++){sum=sum+arr[i];}System.out.println(sum);// 1500}}
Enter fullscreen modeExit fullscreen mode

Arrays are very useful in programming. Think of an array like a group of variables, all with the same name and data type. Use an index to access each element. You can assign a value to an element of the array or access the value of an element, just like plain variables. Use arrays with loops to see the true power of arrays.

Thanks for reading.

Follow me on Twitter@realEdwinTorres for more programming tips. 😀

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Department Chief Engineer @MITREcorp. CS professor @monmouthu. Proud husband and father ❤️. My tweets are my own.
  • Location
    NJ
  • Education
    Doctor of Engineering, George Washington University
  • Work
    Principal Software Engineer at MITRE
  • Joined

More fromEdwin Torres

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp