input license here

I Learn to Code - News Boss

Arrays are a declarative type that is quite difficult to use for those just starting to learn programming.


What is an array? It is used for what and how in programming?


You are viewing: What is an array?


In this article, we will learn about the most basic array type - a 1-dimensional array in Java:


Article overview



  1. Array definition

  2. How to declare a 1-dimensional array?

  3. How to access/reference an element of a one-dimensional array

  4. List the elements of a 1-dimensional array using a basic loop.


What is an array?


An array is a set consisting of a fixed number elements with the same datatypes, used to data saving.


Note: because the number of elements in the array is a fixed number, after declaring the array, we cannot add a new element or delete the current element.


How to declare a one-dimensional array?


According to the definition of array, to declare an array we need:



  • Array data types: for example int, String, boolean, float, double, etc.

  • The number of elements of the array

  • The name of the array


Suppose we need to declare an array with integer data type, consisting of 5 elements, named arr


In Java we have 2 ways of declaration:



int[] arr = {1,2,3,4,5}; // according to the standard, people often use this kb method


or int arr[] = {1, 2, 3, 4 ,5};


Hot: What is Intern, What is Internship? Ways to score points with the company for trainees


This method is used when we already know that the array has exactly 5 elements: 1, 2, 3, 4, 5


Another example: String array: String arr = {“a”, “b”, “c”};



int[] arr = new int[5];


This method is used if we do not know in advance what numbers this integer array consists of.


We can see the number of elements of the array by: arr.length


How to access/reference an element of a one-dimensional array


The elements sorted in the array are numbered according to the number (index).


The first element of the array has an index of 0 .


The last element of the array whose index is the number of array elements – 1


With integer array {1, 2, 3, 4, 5}, we have:


array-1.JPG


arr[0] = 1;


arr[1] = 2;


Related Content: Aztec – a mysterious civilization


arr[2] = 3;


arr[3] =4;


arr[4] =5;


There is no element with index less than 0 or greater than 4, if we intentionally refer to it, the program will report an error (java.lang.ArrayIndexOutOfBoundsException is thrown), this error is common when we work. with arrays or lists.


In general, to refer to an i-th element of an array:


arr[i] with 0 <= i <= arr.length – 1 or (0 <= i < arr.length )


Enumerate the elements of a one-dimensional array using a basic loop


Because we always know the number of elements in the array, we often use a for loop that uses an index to list:


or


(here i act as index of array arr so 0 <= i < number of elements of array )


There is another way using the for loop to list the elements of the array without using the index:


That's what about one-dimensional arrays that I want to introduce to you.


By this point, the article is already quite long, I know some of you don't like it long, so I'll try to make it as short and meaningful as possible. See you in the next part: Java Array Declaration – Two-dimensional arrays and other multi-dimensional arrays


See also: What is Kardiachain? Learn about Kardiachain project and KAI Coin?


Related Posts
Diệp Quân
Nguyen Manh Cuong is the author and founder of the vmwareplayerfree blog. With over 14 years of experience in Online Marketing, he now runs a number of successful websites, and occasionally shares his experience & knowledge on this blog.
SHARE

Related Posts

Subscribe to get free updates

Post a Comment

Sticky