Skip to content

Commit

Permalink
Fix <TOCInline /> (table of contents)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mojib Mohammad authored and Mojib Mohammad committed Mar 4, 2022
1 parent c932cc4 commit 4c4a32b
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions data/blog/array.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ Suppose you were told to build a software to keep track of all the DVDs in an in
This is the exact same scenario that we just described above, but on a much larger scale. So let's imagine the DVDs box as a virtual DVDs library. For each DVD, you would have certain properties that would be specific attributes of the DVDs themselves.
In addition to the properties of a DVD, you're also told the maximum number of DVDs that can be stored in the inventory. Obviously, you wouldn't want to store ancient DVDs from the 1900s unless the were popular ones, right? Say you were told that the requirement is to maintain a maximum inventory of just 100 DVDs. This is an important piece of information because, without this, you wouldn't be able to find the perfectly sized box to fit all the DVDs easily. How could we find a box of a particular sie that would be able to fit a maximum of 100 DVDs? Well, lucky for us, we don't need to physically find a cardboard box or anything-there's a programming construct for this purpose. That programming construct is known as an `Array`.

## Table of Contents

<TOCInline toc={props.toc} asDisclousure />

## Creating an Array
Expand Down Expand Up @@ -55,7 +53,7 @@ Well, the reason is the same as it is for physical cardboard box of DVDs. Do you

It's exactly the same with Array in computer, You don't want to waste space when you might never you use it.

## Accessing Elements in Arrays
### Accessing Elements in Arrays

> The two primitive Array operations are writting elements into them, and reading elements from them. Alll other Array operations are built on top of these two primitive operations.
Expand Down Expand Up @@ -83,7 +81,7 @@ movies[3] = "Star Wars";
movies[1] = "The Lion King";
```

## Reading Items from an Array
### Reading Items from an Array

We can check what's at a particular Array index.

Expand All @@ -101,7 +99,7 @@ System.out.println(movies[1]);

Notice that because we haven't yet put anything at index 4, the value it contains is `null`. In other languages, such as **C**, the Array slot could contain completely random data. Java always initializes empty Array slots to `null` if the Array contains objects, or to default values if it contains primitive types. For example, the array `int [] `would contain the default value of `0` for each element, `float[]` would contain default values of `0.0`, and `bool[]` would contain default values of `false`.

## Writting Items into an Array with a Loop
### Writting Items into an Array with a Loop

We commonly use a loop to put lots of values into an Array. To illustrate this, let's go to another example. This time, we're going to create an Array of `int`s and put the first `10` square numbers inot it.

Expand All @@ -119,7 +117,7 @@ for (int i = 0; i < 10; i++) {
}
```

## Reading items from an Array with a Loop
### Reading items from an Array with a Loop

We can also use a loop to print out everything that's in the Array.

Expand Down Expand Up @@ -157,7 +155,7 @@ for (int square : squareNumbers) {

You'll probably agree that this code is a lot simpler to read. We can use it whenever we don't need the index values. For actually writing the squares into the Array, it wouldn't have worked because we needed to work with the actual index numbers. You don't have to use a "for each" loop when you're starting out, but we recommend you become comfortable with it before interviews. Simple, elegant code is good code!

## Know ahead of time what you will store in to the Array
### Know ahead of time what you will store in to the Array

If you know what you are going to store in the Array ahead of time, Than there a more elegant way to initialize an Array and at the same time store your elements.

Expand Down

1 comment on commit 4c4a32b

@vercel
Copy link

@vercel vercel bot commented on 4c4a32b Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.