Java Arrays: an indebt explanation for beginners, Lecture notes of Computer Science

Java arrays Course code: COM 123 source: internet Year: unverified

Typology: Lecture notes

2022/2023

Uploaded on 06/30/2024

ahembe-jerry
ahembe-jerry ๐Ÿ‡ณ๐Ÿ‡ฌ

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays
Chris Piech
CS106A, Stanford University
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download Java Arrays: an indebt explanation for beginners and more Lecture notes Computer Science in PDF only on Docsity!

Arrays

Chris Piech CS106A, Stanford University

Changing Variable Types int to String? int x = 5; String xStr = โ€œโ€ + x String to int? String xStr = โ€œ5โ€; int x = Integer.parseInt(x); int to double? int x = 5; double xDbl = x; String to double? String xStr = โ€œ5.6โ€; double x = Double.parseDouble(xStr); Casting double to int double x = 5.2; int y = (int)x; GObject obj = getElementAt(5, 2); GRect objRect = (GRect)obj; GObject to GRect int diff = โ€˜C'-โ€™Aโ€™; char next = (char)โ€˜aโ€™ + diff; int to char

Thanks Keith Schwarz for some great slides to build off! Winter is Coming!

Karel Wars Thanks to Nick Troccoli for the awesome example

list.size() Returns the number of values in the list. list.isEmpty() Returns true if the list is empty. list.set(i, value) Sets the i th entry in the list to value. list.get(i) Returns the i th entry in the list. list.add(value) Adds a new value to the end of the list. list.add(index, value) Inserts the value before the specified index position. list.remove(index) Removes the value at the specified index position. list.clear() Removes all values from the list. Common ArrayList methods

  • The Java ArrayList class is derived from an older, more primitive type called an array , which is a collection of individual data values with two distinguishing characteristics:
  • As with array lists, the individual values in an array are called elements , the type of those elements (which must be the same because arrays are homogeneous) is called the element type , and the number of elements is called the length of the array. Each element is identified by its position number in the array, which is called its index. An array is ordered. You must be able to count off the values: here is the first, here is the second, and so on.

An array is homogeneous. Every value in the array must have the same type.

Arrays in Java

  • Arrays are built into the Java language and offer a more expressive selection syntax.
  • You can create arrays of primitive types like int and double and therefore donโ€™t need to use wrapper types like Integer and Double.
  • It is much easier to create arrays of a fixed, predetermined size.
  • Java makes it easy to initialize the elements of an array.
  • Many methods in the Java libraries take arrays as parameters or return arrays as a result. You need to understand arrays in order to use those methods. Why use arrays?
  1. Fantastic style
  2. You will be a better programmer if you understand your roots

A new variable type that is an object that represents an

ordered, homogeneous list of data.

  • Arrays have many elements that you can access using indices index 0 1 2 3 4 5 6 7 8 9 value 12 49 - 2 26 5 17 - 6 84 72 3 element 0 element 4 element 9 length = 10 Arrays #majorkey of the day

Creating an Array type [] name = new type [ length ];

int[] numbers = new int[5];

index 0 1 2 3 4 value 0 0 0 0 0

Java automatically initializes elements to 0.

Crea5ng Arrays

Putting Data In An Array name [ index ] = value ; // set element at index Setting values

Putting Data In An Array name [ index ] = value ; // set element at index

  • Like Strings, indices go from 0 to the array's length - 1. int[] numbers = new int[7]; for (int i = 0; i < 7; i++) { numbers[i] = i; } numbers[8] = 2; // exception numbers[-1] = 5; // exception index 0 1 2 3 4 5 6 value 0 1 2 3 4 5 6 Setting values

Arrays Of Other Types

You can create arrays of any variable type. For example:

double [] results = new double [5];

String[] names = new String[3];

boolean [] switches = new boolean [4];

GRect[] rects = new GRect[5];

  • Java initializes each element of a new array to its default value , which is 0 for int and double, โ€˜\0โ€™ for char, false for boolean, and null for objects. Many flavors of arrays

Array Length Similar to a String, you can get the length of an array by saying

myArray .length

Note that there are no parentheses at the end!

Prac%ce:

  • What is the index of the last element of an array in terms

of its length?

  • What is the index of the middle element of an array in

terms of its length?

Ge=ng โ€œlengthโ€