4.1.2
Arrays & Strings
Arrays
Arrays
Arrays are really useful when you have access to large amounts of related data, but you don't want to store in multiple variables.
![Illustrative background for Arrays](https://image-v2.cdn.app.senecalearning.com/2018-08/61d41c25-a8c0-4b32-b101-5487ab592e7a/computer-code-,h_400,q_80,w_640.jpg)
![Illustrative background for Arrays ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/61d41c25-a8c0-4b32-b101-5487ab592e7a/computer-code-,h_400,q_80,w_640.jpg)
Arrays
Arrays
- An array is a data structure that stores a fixed number of values under a single identifier.
- The values must be of the same type.
![Illustrative background for Array elements](https://image-v2.cdn.app.senecalearning.com/2018-08/7bee6a10-8009-4d5f-ab3d-239dfe82b6b1/numbers-1487223_1280,h_400,q_80,w_640.png)
![Illustrative background for Array elements ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/7bee6a10-8009-4d5f-ab3d-239dfe82b6b1/numbers-1487223_1280,h_400,q_80,w_640.png)
Array elements
Array elements
- Each piece of data inside an array is called an element.
- Each element has an index, which is its position within the array.
- We start counting the indices from 0.
![Illustrative background for Arrays in pseudocode](https://image-v2.cdn.app.senecalearning.com/2018-08/d53c1aa9-0297-466f-9cff-c0f81452a85f/technology-1283624_1920,h_400,q_80,w_640.jpg)
![Illustrative background for Arrays in pseudocode ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/d53c1aa9-0297-466f-9cff-c0f81452a85f/technology-1283624_1920,h_400,q_80,w_640.jpg)
Arrays in pseudocode
Arrays in pseudocode
- An array of ten items can be created using the following syntax:
- array name_of_array[10]
- Values can be assigned as follows:
- name_of_array[0] = "Hello"
- Values can be accessed as follows:
- print(name_of_array[0])
2D Arrays
2D Arrays
A 2D array is the data structure which consists of an array of arrays.
![Illustrative background for 2D Array](https://image-v2.cdn.app.senecalearning.com/2018-08/789909dc-f5d8-4151-97cf-5012e4f86af3/coding-1853305_1920,h_400,q_80,w_640.jpg)
![Illustrative background for 2D Array ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/789909dc-f5d8-4151-97cf-5012e4f86af3/coding-1853305_1920,h_400,q_80,w_640.jpg)
2D Array
2D Array
- A 2D array is a data structure where arrays are filled with other arrays.
- This can be used to represent a 2D surface, or records.
![Illustrative background for Accessing elements](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet,h_400,q_80,w_640.jpg)
![Illustrative background for Accessing elements ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/95d17dd9-261f-4238-9065-f411ea8c7210/code-Data-Computer-Wifi-Internet,h_400,q_80,w_640.jpg)
Accessing elements
Accessing elements
- To access an individual array, we can access the array as usual:
- array = my2DArray[4]
- To access an individual element, we use this syntax:
- array[5][0]
- This would get the first element of the sixth array.
![Illustrative background for Applications of 2D arrays](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/10.3.1 Using materials/pexels-photo-65169-min,h_400,q_80,w_640.jpg)
![Illustrative background for Applications of 2D arrays ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/10.3.1 Using materials/pexels-photo-65169-min,h_400,q_80,w_640.jpg)
Applications of 2D arrays
Applications of 2D arrays
- 2D arrays are often used to represent a 2D surface, for example a chess board:
- Each element of the 2D array could contain the chess piece in that location.
- 2D arrays can be used to represent a database:
- The outer array stores a collection of records.
- The inner arrays store a single record.
Strings
Strings
Strings are special cases of arrays or lists, where each element is a character. There are many methods we can apply to them:
![Illustrative background for Concatenation](https://image-v2.cdn.app.senecalearning.com/2018-08/eeeae8d9-efee-41bc-8e5e-23daff64465b/earth-1617121_1920,h_400,q_80,w_640.jpg)
![Illustrative background for Concatenation ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/eeeae8d9-efee-41bc-8e5e-23daff64465b/earth-1617121_1920,h_400,q_80,w_640.jpg)
Concatenation
Concatenation
- Concatenation is joining two strings together.
- This can be done with the + sign. E.g:
- "Hello " + "World" = "Hello World".
![Illustrative background for Accessing a character](https://image-v2.cdn.app.senecalearning.com/2018-08/33935250-d722-41a3-a9cf-588036929f55/checklist-2077020_1920,h_400,q_80,w_640.jpg)
![Illustrative background for Accessing a character ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/33935250-d722-41a3-a9cf-588036929f55/checklist-2077020_1920,h_400,q_80,w_640.jpg)
Accessing a character
Accessing a character
- Remember, strings are just lists. So we can access a specific character in the exact same way as we access a list element:
- string[3] would get the fourth character.
![Illustrative background for Length of a string](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA New Modules/1.1.2/string force,h_400,q_80,w_640.jpg)
![Illustrative background for Length of a string ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA New Modules/1.1.2/string force,h_400,q_80,w_640.jpg)
Length of a string
Length of a string
- The length of a string is given by the len function:
- len("Hello") would give 5.
![Illustrative background for Substrings](https://image-v2.cdn.app.senecalearning.com/2018-08/772dc79d-aa48-4875-a604-fa06b95561f9/rope-160161_1280,h_400,q_80,w_640.png)
![Illustrative background for Substrings ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/772dc79d-aa48-4875-a604-fa06b95561f9/rope-160161_1280,h_400,q_80,w_640.png)
Substrings
Substrings
- A substring is a part of a longer string.
- In pseudocode, we use the function:
- stringname.subString(startPos, numberOfChars)
- This takes a substring starting at the 'startPos'th character, and takes the stated number of characters.
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
Jump to other topics
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered