知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

      • Theory:Computer algorithms
      • Theory:Pseudocode
      • Theory:Pseudocode basics
      • Theory:Complex constructions in pseudocode
      • Theory:The big O notation
      • Theory:Best, Average and cases
      • Theory:Data structures
      • Theory:Fixed-size array
        • Fixed-size array
        • Accessing elements
        • Inserting and deleting elements
        • Example
        • Conclusion
      • Theory:Dynamic array
    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Algorithm & Data structure
Jim
2022-09-23
目录

Theory:Fixed-size array

In programs, data is represented in the form of numbers, characters, strings, or other more complex objects. Often, some of these objects can be naturally grouped together. For example, assume that you conducted an experiment and got some measurements. They might correspond to temperature, distance, time, or something else. In such a case, it would be convenient not to store each measurement as a separate variable, but to process all of them together as a single unit. This will neatly organize our experimental observations, allowing us to analyze them quickly.

To efficiently deal with such cases, most programming languages provide a structure called a fixed-size array. The structure allows us to sequentially store a collection of elements of the same type and process them as a single unit. In this topic, we will look at this structure in more detail and learn some basic operations used for arrays.

# Fixed-size array

A fixed-size array is a structure for storing a collection of elements of the same type. As you can guess from its name, the size of such an array is a constant: once an array is created, you can't change its size. While creating a fixed-size array, we declare its size. The computer then reserves necessary memory resources for the array. After that, the elements of a fixed-size array are stored sequentially into those memory addresses. Given below is an example of a fixed-size array that stores five floating-point numbers:

img

An array isn't limited to storing numeric values only. We can also store a list of strings in it. Like this one containing some flower names:

img

Arrays have some of technical characteristics. To begin with, the size of an array indicates how many elements the array contains. It is also referred to as the length of an array. The length of both of our previous arrays is 5.

The index of an element is a number that tells us where the element resides within the array. For most programming languages, the counting starts at 00. The first element of the first array is 10.810.8 and its index is 00, the second one is 14.314.3 with the index of 11. The last element is 9.79.7 with the index of 44. The same rule applies to the second array as well.

Using pseudocode, we can represent the first array as follows:

measurements = [10.8, 14.3, 13.5, 12.1, 9.7]
1

A variable named measurementsmeasurements combines the numbers in a single unit.

# Accessing elements

Programming languages provide a set of standard methods for array processing. There is one of them used most frequently. It is a method for accessing an element by its index. Let's try and access the third element of the measurementmeasuremen**t array and store it in a new variable valuevalue.

img

The valuevalue now contains 13.513.5.

Take notice that we can not only read, but also modify elements of an array:

measurements[2] = 3.7
1

Now, the array looks like this:

img

Both reading and modifying operations require O(1)O(1) time complexity. It's so efficient, because by knowing the index number, the computer can jump directly to the required memory address and fetch or modify the data.

# Inserting and deleting elements

In short, inserting an element into a fixed-size array or deleting an element from the array is not possible. This is mainly because those operations would change the length of the array and it would no longer be a fixed-size array.

Still, you may want to add one more flower named DaisyDaisy to the array of flowers mentioned earlier. There's a way to do so! After inserting, the length of our new array will be 66. Thus, you need to create another array with the length 66, and copy all the five elements from the first array to the new array. There will be a spot left in your new array. Fill it up with the new flower name. You can do the same trick for deleting an element as well.

img

This operation requires O(n)O(n) time complexity, where nn is the number of elements of the array, since we have to copy all nn elements to our new array. In sense of performance, inserting and deleting are very slow operations for a fixed-size array. To overcome this limitation, programmers have introduced dynamic arrays that you will learn about later.

On the flip side, the inability to modify the size is a strong characteristic of a fixed-size array. Nothing can affect our array's length. In the example above, we've added a new flower name to the array, but still, our old array remains as it is. Thus, it is wise to use a fixed-size array when changing the array length may negatively affect your program.

Along with these operations, there are some other more sophisticated methods for array processing, such as sorting, reversing, searching elements, and others. When you use a particular programming language, check the documentation of the standard library to see what methods the language provides.

# Example

Let's consider a simple example of how to process arrays. Given an array of numbers, our task will be to calculate the mean value of the elements in the array. The mean of the array elements is the sum of all array elements divided by their number. We will consider how it can be done for our array of measurements:

measurements = [10.8, 14.3, 13.5, 12.1, 9.7]
1

The procedure is the following:

We initialize the variable sumsum with the value 00. Then, we sequentially read the elements of the measurementmeasuremen**t array using the index numbers from zero up to the array length minus one, which is the index of the last element, and add them to the sumsum variable. After that, we divide the obtained sum by the length of the array and thus get the mean value for the elements. The length of the array of measurements is known in advance.

Here is the pseudocode of the process:

sum = 0

for i from 0 to (len(measurements)- 1):
    sum = sum + measurements[i]

mean = sum / len(measurements) # 12.08
1
2
3
4
5
6

Here, len(measurements)len(measurements) will return the length of the measurements array.

# Conclusion

Let's now summarize the main points considered in this topic.

The array data structure is widely used in programming. A fixed-size array allows us to store a collection of elements of the same type. The most frequently used method of array processing is accessing an element by its index. Not all array methods are efficient for a fixed-size array, but still, we can use them to our advantage. Since an array is a collection of data of the same type, processing it is easy and intuitive. Without this data structure, we would have to declare a new variable for every value in a list, which would result in a complex program and require much more storage.

To get information about other array methods, check the standard library of a programming language you use. Use a fixed-size array if you need to process a collection of data of a similar type and you know the number of values in advance.

编辑 (opens new window)
#Data structure
上次更新: 2022/09/26, 16:55:15
Theory:Data structures
Theory:Dynamic array

← Theory:Data structures Theory:Dynamic array→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式