Array

本文详细介绍了数组的概念,包括零基、一基和n基索引,并探讨了使用数组的优势,如随机访问和缓存局部性。分别讨论了C/C++和Java中数组的声明、初始化、访问元素、数组边界检查、动态分配和连续存储位置等方面。还对比了C/C++和Java中数组的区别,并提到了Java中的动态分配、数组长度成员以及多维数组的使用。
摘要由CSDN通过智能技术生成

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). 数组是元素的集合,存储在连续的内存空间,类型一样,有利于计算每个元素的位置,在基数上增加偏移即可。

Types of indexing in array:

  • 0 (zero-based indexing): The first element of the array is indexed by subscript of 0  下标从0开始的
  • 1 (one-based indexing): The first element of the array is indexed by subscript of 1  下标从1开始的
  • n (n-based indexing): The base index of an array can be freely chosen. Usually programming languages allowing n-based indexing also allow negative index values and other scalar data types like enumerations, or characters may be used as an array index.

Advantages of using arrays:

  • Arrays allow random access of elements. This makes accessing elements by position faster.  array可以随机访问,
  • Arrays have better cache locality that can make a pretty big difference in performance. 

Examples –

// A character array in C/C++/Java
char arr1[] = {'g', 'e', 'e', 'k', 's'};

// An Integer array in C/C++/Java
int arr2[] = {10, 20, 30, 40, 50};

// Item at i'th index in array is typically accessed
// as "arr[i]".  For example arr1[0] gives us 'g'
// and arr2[3] gives us 40.

Arrays in C/C++

An array is collection of items stored at continuous memory locations.
arrays

Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we have small number of objects, but if we want to store large number of instances, it becomes difficult to manage them with normal variables. The idea of array is to represent many instances in one variable.

Array declaration in C/C++:


 

We can declare an array by specifying its type and size or by initializing it or by both.

  1. Array declaration by specifying size
// Array declaration by specifying size
int arr1[10];
  

// With recent C/C++ versions, we can also
// declare an array of user specified size
int n = 10;
int arr2[n];
  1. Array declaration by initializing elements
// Array declaration by initializing elements 
int arr[] = { 10, 20, 30, 40 } 
  
// Compiler creates an array of size 4. 
// above is same as  "int arr[4] = {10, 20, 30, 40}" 
  1. Array declaration by specifying size and initializing elements
// Array declaration by specifying size and initializing 
// elements 
int arr[6] = { 10, 20, 30, 40 } 
  
// Compiler creates an array of size 6, initializes first 
// 4 elements as specified by user and rest two elements as 0. 
// above is same as  "int arr[] = {10, 20, 30, 40, 0, 0}" 

Facts about Array in C/C++:

  • Accessing Array Elements:
    Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1.

    Following are few examples.


#include <stdio.h> 
  
int main() 
{ 
    int arr[5]; 
    arr[0] = 5; 
    arr[2] = -10; 
    arr[3 / 2] = 2; // this is same as arr[1] = 2 
    arr[3] = arr[0]; 
  
    printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]); 
  
    return 0; 
} 
  • Output:
    5 2 -10 5
    
  • No Index Out of bound Checking:
    There is no index out of bound checking in C, for example the following program compiles fine but may produce unexpected output when run.
// This C program compiles fine 
// as index out of bound 
// is not checked in C. 
  
#include <stdio.h> 
  
int main() 
{ 
    int arr[2]; 
  
    printf("%d ", arr[3]); 
    printf("%d ", arr[-2]); 
  
    return 0; 
} 
  • Output:
    2008101287 4195777
    
  • Also, In C, it i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值