From ad3ce4f6beee76c4cdb19f8ae868a514afe351ae Mon Sep 17 00:00:00 2001 From: Arghya Ghosh <71373838+uiuxarghya@users.noreply.github.com> Date: Thu, 12 Oct 2023 19:17:48 +0530 Subject: [PATCH 1/2] Update documentation.js --- src/navs/documentation.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/navs/documentation.js b/src/navs/documentation.js index 1ff6d97..980e210 100644 --- a/src/navs/documentation.js +++ b/src/navs/documentation.js @@ -27,4 +27,5 @@ export const documentationNav = { pages['break-statement'], pages['continue-statement'], ], + 'Java Arrays': [pages['arrays']], } From 48d67629ba4d9a4f708eb2a039b8c7fa87f03c3c Mon Sep 17 00:00:00 2001 From: Arghya Ghosh <71373838+uiuxarghya@users.noreply.github.com> Date: Thu, 12 Oct 2023 19:17:53 +0530 Subject: [PATCH 2/2] Create arrays.mdx --- src/pages/docs/arrays.mdx | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/pages/docs/arrays.mdx diff --git a/src/pages/docs/arrays.mdx b/src/pages/docs/arrays.mdx new file mode 100644 index 0000000..5b933c0 --- /dev/null +++ b/src/pages/docs/arrays.mdx @@ -0,0 +1,78 @@ +--- +title: Java Arrays +description: In this tutorial, we will learn how to use the Java continue statement to skip the current iteration of a loop. +--- + +## Java Arrays +An array is a collection of similar types of data. + +For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. + +```java +String[] array = new String[100]; +``` +Here, the above array cannot store more than 100 names. The number of values in a Java array is always fixed. + +### How to declare an array in Java? +In Java, here is how we can declare an array. + +```java +dataType[] arrayName; +``` +- `dataType` - it can be primitive data types like `int`, `char`, `double`, `byte`, etc. or Java objects +- `arrayName` - it is an identifier +For example, + +```java +double[] data; +``` +Here, data is an array that can hold values of type double. + +But, how many elements can array this hold? + +Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example, + +```java +// declare an array +double[] data; + +// allocate memory +data = new double[10]; +``` +Here, the array can store 10 elements. We can also say that the size or length of the array is 10. + +In Java, we can declare and allocate the memory of an array in one single statement. For example, + +```java +double[] data = new double[10]; +``` +### How to Initialize Arrays in Java? +In Java, we can initialize arrays during declaration. For example, + +```java +//declare and initialize and array +int[] age = {12, 4, 5, 2, 5}; +``` +Here, we have created an array named age and initialized it with the values inside the curly brackets. + +Note that we have not provided the size of the array. In this case, the Java compiler automatically specifies the size by counting the number of elements in the array (i.e. 5). + +In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number. For example, + +```java +// declare an array +int[] age = new int[5]; + +// initialize array +age[0] = 12; +age[1] = 4; +age[2] = 5; +.. +``` +Elements are stored in the array +Java Arrays initialization + +Note: + +Array indices always start from 0. That is, the first element of an array is at index 0. +If the size of an array is n, then the last element of the array will be at index n-1. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy