ICS3U1 - Unit 3: Strings and Arrays

0.0(0)
studied byStudied by 11 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/38

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

39 Terms

1
New cards

Strings

Important variable type that stores words and numbers, but cannot do any arithmetic operation

2
New cards

Characters

An 8 bit data type that stores a letter, number, symbol, space, etc.

3
New cards

Difference between integer, character, and string (in terms of syntax)

Integer: 3

Character: ‘3’

String: “3”

4
New cards

String Class

A class that includes numerous methods that can manipulate strings

5
New cards

str.charAt(index);

Returns a character at a specific index

6
New cards

str.length();

Finds the total length of the string (outputs an integer)

7
New cards

str.substring(int start); and str.substring(int start, int end);

Allows the Java to point at a specific index and output the remaining characters following that index.

  • Can pinpoint a start and end point

8
New cards

str.equals();

Comparing string values with another

  • Outputs a boolean value

9
New cards

str.equalsIgnoreCase();

Compares strings, but ignores the casings

  • Outputs boolean value

10
New cards

Index

A numerical assignment of each character in the string

  • Starts at 0, and is 1 less than the total length of the string

11
New cards

Errors with Indices

Since the final index is 1 less than the total length of the string, an error will occur if you input an index value greater than or less than what is there.

  • Anything < 0

  • Anything > str.length();

These are known as IndexOutofBound Errors

12
New cards

ASCII Table

American Standard Code for Information Interchange

  • A table of characters for the computer

13
New cards

Characters /w the ASCII Table

Every character is assigned a numerical value

  • Ex: ‘a’ has a value of 97

14
New cards

Case Sensitivity with Characters

Although we see ‘a’ and ‘A’ as the same letter but different casings, Java sees it as ‘97’ and ‘65’.

  • Meaning java casing matters in Java

15
New cards

Casting

Casting a data type allows Java to output either a number or a character value depending on what you ask for

16
New cards

Increments Dealing with Characters

These are not the Same:

  • letter++;

  • letter = letter + 1;

letter = letter + 1;

  • This is trying to add a character and an integer together

17
New cards

CompareTo() vs equals()

Both compare 2 different strings, however, they are slightly different

CompreTo()

  • Returns 0 if they are the same and returns less than or greater than 0 depending on the order.

  • CompareTo() analyzes it alphabetically

Equals()

  • Returns a boolean value (true or false)

18
New cards

Exception

An error that disrupts the flow of the code

19
New cards

Try-Catch Statements

A block of code that will catch any errors that may occur within the statement.

  • It will only catch the errors within the Try statement

<p>A block of code that will catch any errors that may occur within the statement.</p><ul><li><p>It will only catch the errors within the <strong>Try</strong> statement</p></li></ul>
20
New cards

Common use of Try-Catch Statements

InputMismatch Error

  • This is when the use inputs a different variable type than the one that is needed.

21
New cards

Exception-Handler

A try-catch statement is broken up into 2 parts

Try - Exception

  • The block of code that is being checked for errors

Catch - Exception Handler

  • Catches the error and handles it

22
New cards

Array

A data structure that can hold multiple of the same data type together.

23
New cards

Elements

This is the data within the array

24
New cards

Difference between variable and an array

An array points to a memory location which is why it is known as an object.

  • If we simply print the array, it will output the memory address of the array

25
New cards

How does an array work

An array splits up the elements using indices. Just like a string, every element has an index assigned to it.

26
New cards

Different parts of an array

int[] grades = new int[20];

Data Type: int

Array indicator: []

Name: Grades

Size: [20]

<p>int[] grades = new int[20];</p><p><strong>Data Type:</strong> int</p><p><strong>Array </strong>indicator: []</p><p><strong>Name:</strong> Grades</p><p><strong>Size: </strong>[20]</p>
27
New cards

Syntax to Declare

int[] marks;

marks = new int[5];

<p>int[] marks;</p><p>marks = new int[5];</p>
28
New cards

Initializing an array

An array can be preset values to it, however, the syntax is a little different. Everything will be the same except after declaring the size you will use curly brackets {}, and write the values you want inside dividing it using commas.

<p>An array can be preset values to it, however, the syntax is a little different. Everything will be the same except after declaring the size you will use curly brackets {}, and write the values you want inside dividing it using commas.</p>
29
New cards

Accessing elements within an array

Write the name of the array and surround the index/element with square brackets.

<p>Write the name of the array and surround the index/element with square brackets.</p>
30
New cards

Changing a value in an array

Access the element you want and treat it as a variable

<p>Access the element you want and treat it as a variable</p>
31
New cards

Length of an Array

Syntax is very similar to a string, thus being:

array.length;

  • Only difference is the array doesn’t have brackets at the end while strings do

32
New cards

Index Errors

Just like strings, you cannot ask for an element/index that does not exist. There will be an error telling you that element does not exist.

  • ArrayIndexOutofBounds Exceptions

33
New cards

Method Set Up

Access Levels:

  • Public

  • Private

Return Type:

  • Void

  • Data Types

Address:

  • Method name

Parameters:

  • The variables you want in the method

<p><strong>Access Levels:</strong></p><ul><li><p>Public</p></li><li><p>Private</p></li></ul><p><strong>Return Type:</strong></p><ul><li><p>Void</p></li><li><p>Data Types</p></li></ul><p><strong>Address:</strong></p><ul><li><p>Method name</p></li></ul><p><strong>Parameters:</strong></p><ul><li><p>The variables you want in the method</p></li></ul>
34
New cards

Void Method

Does not return values back to the main method, so whatever is in the method stays in the method.

  • Key word return is not needed

35
New cards

Passing data into methods

When initializing the method you only have to write the name of the variable in the brackets. However within the parameters of the method, the variable’s data type must line up with the ones on the initializer.

  • Order matters

<p>When initializing the <strong>method </strong>you only have to write the name of the variable in the brackets. However within the parameters of the method, the variable’s data type must line up with the ones on the initializer. </p><ul><li><p><strong>Order matters</strong></p></li></ul>
36
New cards

Data within a method

The passed data is not the same as the data in main. Although in some cases the variable name is the same, they are not the same variable. Meaning whatever changes are made within the method, will only affect the variable in it and not main.

37
New cards

Return in Method

This is used when we want to return a value back into main using method.

  • The data type you are returning must match the return type in the method header

38
New cards

Arrays in Method

When passing arrays into methods this is known as a PASS-BY REFERENCE. This is because an array is an object that has a memory location/memory address.

  • Changes made to the array in a method will also be changed in main since both point to the same memory location

39
New cards

Pass By Value VS Pass By Reference

Pass By Value: when a variable is assigned to another variable, the value stored in the variable is copied into the new variable.

Pass By Reference: Passing objects to a method.