SlideShare a Scribd company logo
Generative Coding
arrays
• Using variables , we can store information at one step in the program
for use at a later time. We store a single value per variable. Arrays now
allow us to cache multiple values ​​at once. An array is a list of
variables where each element has the same data type . An array is a
data structure.
Declaration of an array
• Arrays must be declared like variables before they can be used, except that there are square brackets after the data type:
// Declaration of an integer variable
int number;
// declaration of an integer array
int [] numbers;
To initialize an array, we need to define the length of the array . The length determines how many elements can be stored in the array.
In the following example we define our array numbers so that five elements (of type int) can be stored in it, and the other array positionen so that we float
can store 30 elements, i.e. 30 values.
// Array with five integer elements
int [] numbers = new int [5];
// Array of thirty point number elements
float [] positions = new float [30];
An array is initialized like this: new data type[length]; so for example new float[4];
Access to individual items
• We can access each item through an index . This index is always an integer and starts with 0 . Each element
is of the same data type and can be used like a normal variable. When we access a single element of the array,
we can read and write its value.
• Here we see how to assign new values ​​to the elements of an array
int x = 5;
int [] numbers = new int [5];
numbers[0] = 100;
numbers[1] = 255;
numbers[2] = 2;
numbers[3] = width ;
numbers[4] = 10 * x;
Access to individual items
• We can use the individual elements as well as variables:
int x = new numbers[4];
int y = 4;
y = y + numbers[3];
line (x, y, numbers[1], numbers[2]);
• And of course we can also use a variable or expressions for the index:
int number = 10;
// Be it initialization
char [] character = new char [number];
// Or when accessed
int i = 5;
numbers[i] = numbers[2 + 1];
Arrays & Loops
• When using or computing a lot of similar data, arrays are very useful.
• For example, if we wanted to animate a ball, we had to store its
position and speed in variables: x, y, vx, vy. If not just one but several
balls are to be displayed, you would have to use four variables per ball
( x2, y2, vx2, vy2, x3, y3, ...), which would be very tedious. Instead, we
can use arrays.
• So the purpose of arrays is that we can store many of the same kind
of variables together — and save us a lot of unnecessary typing and
programming.
Using arrays
• In order to access each element, we can either specify each position
individually ( ), or access all of them numbers[3] using a for loop .
• We can then use the counter variable of a for loop directly as an
index:
int [] numbers = new int [5];
for ( int i = 0; i < 5; i++) {
numbers[i] = i * 10;
}
Example:
draws ten circles using the values ​​of two arrays as positions.
float [] x = new float [10];
float [] y = new float [10];
Void setup ( ) {
size (200, 200);
// Initialize all elements of both arrays
for ( int i = 0; i < 10; i++) {
x[i] = random ( width );
y[i] = random ( height );
}
}
void draw () {
// Draw circles using a loop and the arrays
for ( int i = 0; i < 10; i++) {
ellipse (x[i], y[i], 20, 20);
}
]

More Related Content

PDF
javaarray
PPT
Cso gaddis java_chapter8
DOCX
Class notes(week 4) on arrays and strings
PDF
PPT
JavaYDL6
PPTX
Arrays in programming
PDF
Class notes(week 4) on arrays and strings
PDF
Java Arrays
javaarray
Cso gaddis java_chapter8
Class notes(week 4) on arrays and strings
JavaYDL6
Arrays in programming
Class notes(week 4) on arrays and strings
Java Arrays

Similar to Generative Coding Lecture notes using coding (20)

PPT
17-Arrays en java presentación documento
PPT
9 cm604.9
PDF
Java chapter 6 - Arrays -syntax and use
PPT
Arrays Class presentation
PPT
Array1
PPT
Chap09
PDF
Lecture 6 - Arrays
PDF
(2) collections algorithms
PPT
Mesics lecture 8 arrays in 'c'
PDF
C sharp chap6
PPT
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PPT
Array in Java
PPTX
datatypes in java helpfull for collage students.pptx
PPTX
ch 7 single dimension array in oop .pptx
PPT
Eo gaddis java_chapter_07_5e
PPT
Comp102 lec 8
ODP
(2) collections algorithms
PPS
Java session04
PPTX
Chapter 7.1
17-Arrays en java presentación documento
9 cm604.9
Java chapter 6 - Arrays -syntax and use
Arrays Class presentation
Array1
Chap09
Lecture 6 - Arrays
(2) collections algorithms
Mesics lecture 8 arrays in 'c'
C sharp chap6
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Array in Java
datatypes in java helpfull for collage students.pptx
ch 7 single dimension array in oop .pptx
Eo gaddis java_chapter_07_5e
Comp102 lec 8
(2) collections algorithms
Java session04
Chapter 7.1
Ad

Recently uploaded (20)

PPTX
Media And Information Literacy for Grade 12
PDF
Key Trends in Website Development 2025 | B3AITS - Bow & 3 Arrows IT Solutions
PDF
Urban Design Final Project-Context
PDF
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PDF
Urban Design Final Project-Site Analysis
PDF
The Advantages of Working With a Design-Build Studio
PPTX
AD Bungalow Case studies Sem 2.pptxvwewev
PPT
UNIT I- Yarn, types, explanation, process
PDF
YOW2022-BNE-MinimalViableArchitecture.pdf
PPTX
YV PROFILE PROJECTS PROFILE PRES. DESIGN
PPTX
rapid fire quiz in your house is your india.pptx
PPT
WHY_R12 Uaafafafpgradeaffafafafaffff.ppt
PDF
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
PPTX
Fundamental Principles of Visual Graphic Design.pptx
PDF
Quality Control Management for RMG, Level- 4, Certificate
PDF
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
PPTX
An introduction to AI in research and reference management
PPTX
joggers park landscape assignment bandra
PPTX
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
Media And Information Literacy for Grade 12
Key Trends in Website Development 2025 | B3AITS - Bow & 3 Arrows IT Solutions
Urban Design Final Project-Context
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
Urban Design Final Project-Site Analysis
The Advantages of Working With a Design-Build Studio
AD Bungalow Case studies Sem 2.pptxvwewev
UNIT I- Yarn, types, explanation, process
YOW2022-BNE-MinimalViableArchitecture.pdf
YV PROFILE PROJECTS PROFILE PRES. DESIGN
rapid fire quiz in your house is your india.pptx
WHY_R12 Uaafafafpgradeaffafafafaffff.ppt
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
Fundamental Principles of Visual Graphic Design.pptx
Quality Control Management for RMG, Level- 4, Certificate
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
An introduction to AI in research and reference management
joggers park landscape assignment bandra
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
Ad

Generative Coding Lecture notes using coding

  • 2. arrays • Using variables , we can store information at one step in the program for use at a later time. We store a single value per variable. Arrays now allow us to cache multiple values ​​at once. An array is a list of variables where each element has the same data type . An array is a data structure.
  • 3. Declaration of an array • Arrays must be declared like variables before they can be used, except that there are square brackets after the data type: // Declaration of an integer variable int number; // declaration of an integer array int [] numbers; To initialize an array, we need to define the length of the array . The length determines how many elements can be stored in the array. In the following example we define our array numbers so that five elements (of type int) can be stored in it, and the other array positionen so that we float can store 30 elements, i.e. 30 values. // Array with five integer elements int [] numbers = new int [5]; // Array of thirty point number elements float [] positions = new float [30]; An array is initialized like this: new data type[length]; so for example new float[4];
  • 4. Access to individual items • We can access each item through an index . This index is always an integer and starts with 0 . Each element is of the same data type and can be used like a normal variable. When we access a single element of the array, we can read and write its value. • Here we see how to assign new values ​​to the elements of an array int x = 5; int [] numbers = new int [5]; numbers[0] = 100; numbers[1] = 255; numbers[2] = 2; numbers[3] = width ; numbers[4] = 10 * x;
  • 5. Access to individual items • We can use the individual elements as well as variables: int x = new numbers[4]; int y = 4; y = y + numbers[3]; line (x, y, numbers[1], numbers[2]); • And of course we can also use a variable or expressions for the index: int number = 10; // Be it initialization char [] character = new char [number]; // Or when accessed int i = 5; numbers[i] = numbers[2 + 1];
  • 6. Arrays & Loops • When using or computing a lot of similar data, arrays are very useful. • For example, if we wanted to animate a ball, we had to store its position and speed in variables: x, y, vx, vy. If not just one but several balls are to be displayed, you would have to use four variables per ball ( x2, y2, vx2, vy2, x3, y3, ...), which would be very tedious. Instead, we can use arrays. • So the purpose of arrays is that we can store many of the same kind of variables together — and save us a lot of unnecessary typing and programming.
  • 7. Using arrays • In order to access each element, we can either specify each position individually ( ), or access all of them numbers[3] using a for loop . • We can then use the counter variable of a for loop directly as an index: int [] numbers = new int [5]; for ( int i = 0; i < 5; i++) { numbers[i] = i * 10; }
  • 8. Example: draws ten circles using the values ​​of two arrays as positions. float [] x = new float [10]; float [] y = new float [10]; Void setup ( ) { size (200, 200); // Initialize all elements of both arrays for ( int i = 0; i < 10; i++) { x[i] = random ( width ); y[i] = random ( height ); } } void draw () { // Draw circles using a loop and the arrays for ( int i = 0; i < 10; i++) { ellipse (x[i], y[i], 20, 20); } ]