Arrays are often used for scientific calculation or storage of some variables of the same type
The way to write c
#include <stdio.h> int main() { double array1[2] = {1.0, 2.25}; //The element is an array of type double, and the length of the array specified at the time of definition is 2 int array2[] = {1,2,3}; //The element is an array of type int, which can be used if you do not know the number of elements when defining printf("array2[0]=%d\n", array2[0]); //Find elements based on their Subscripts int array3[2][2] = { {1,2}, {3,4} }; //Define a two-dimensional array. If you define a multi-dimensional array, add more brackets printf("array[1][1]=%d\n", array3[1][1]); int array4[3]; //An array is defined but not initialized }
The array in c is not added or deleted
The way to write c++
#include <iostream> #include <string.h> #Include < vector > / / vector header file needs to be introduced using namespace std; int main() { //The array definition in c also applies to c++ int array1[2] = {1,2}; int array2[] = {1,2}; //More convenient vector is added in c + + vector<int> vector1; //Declare a vector vector1 of type int vector<int> vector2(10); //Declare a vector vector2 of type int with an initial size of 10 vector<int> vector3(10,0); //Declare a vector vector3 of type int with an initial size of 10 and an initial value of 0 vector3[0] = 15; //Assign a value to the first element of vector3 std::cout << vector3[0] << std::endl; //View the first element of vector3 vector3.erase(vector3.begin(), vector3.begin()+1); //Delete element in vector std::cout << vector3.size() << std::endl; //Viewing the length of a vector vector3.insert(vector3.begin(),vector3.begin(), vector3.begin()+3); //Insert all elements between vector3.begin() and vector3.begin()+3 before vector3.begin() std::cout << vector3.size() << std::endl; //Viewing the length of a vector vector< vector<int> > vector4(10, vector<int>(5)); //Create a 10 * 5 int type 2D vector }
java writing
public class ArrayDemo { public static void main(String[] args) { int[] array1; //Defines an array of type int, uninitialized int[] array2 = {1,2,3}; array2[0] = 4; //Change element System.out.println(array2[0]); //Find the first element of an array } }
The way to write go
func arrayDemo() { array1 := [5]int{} //Declared array, not array2 := [...]int{1,2,3,4,5} //Declaration array array3 := [2][2]int{{1,2}, {3,4}} //Declare a 2D array println(array1[0]) //Find elements of an array println(array2[0]) println(array3[0][0]) }
The function of slicing in go is similar to list, which will be introduced later. It is not confused with array here
The writing method of python
There is no concept of array in python. If you want to use array, you can directly use list to implement related functions
Comparative summary:
- In addition to python, arrays can be defined by c, c + +, java and go, and arrays can only be changed and searched, not added and deleted directly.
- c + + has more function of vector than c, it is convenient to add, delete, search and modify arrays;
- python has no concept of array;
- c, c + +, java and go need curly braces to define elements when defining arrays (later chapters will explain how to remember and quickly view the syntax of each language);