1.3- Arrays, Array List, and Strings

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/23

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.

24 Terms

1
New cards

Reminder:

  • 8 Primitive types

    • 4 Decimal numbers (byte, short, int, long)

    • 2 Floating Point (float, double)

    • char

    • boolean

Reminder:

Everything else is a complex type

  • generally theyre Objects built upon primitive ← types

2
New cards

String “…”

String color = “blue”;

  • Complex data type

  • list of characters

  • data type is String with capital “S”

3
New cards

Strings are “stored” inside _____

Objects

4
New cards

Objects can contain ___

  • Data attributes

  • Methods (functions)

5
New cards

To access an attribute or call a method inside an object ___

you can use the . operator

ex:

String myColor=”Blue”;
int sizeOfColor=myColor.length()

6
New cards

Java String Manipulation:

•example.toUpperCase()   → “TEST,1,2”

•example.toLowerCase()   → “test,1,2”

•example.trim()  //Removes spaces at front and end

•example.equals(String) → Compare strings

example.equalsIgnoreCase(String) → case insensitive compare

7
New cards

Accessing specific characters in a string

String example=”Test,1,2”;

●example.length() → 8 //There are 8 characters

●example.charAt(0) -> ‘T’ //The first character.

●example.charAt(example.length()-1) -> ‘2’ //Last character

●example.substring(2,5) → “st,”

○Note in Java it’s start position, end position, it doesn’t include the end position.

●example.split(“,”) //returns an array of strings [“Test”,”1”,”2”]

●example.toCharArray() //Produces an array of chars

8
New cards

Collections:

In Python you had lists []’s, tuples ()’s and sets {}’s

In Java you have:

  • Array[]

    • Arrays are fixed in size.  You declare the size when you create them.

    • They can only hold one type at a time.

  • ArrayList

    • They grow and shrink as you need them to. 

    • They likewise can only hold a single type at a time, you declare it when you create them.

9
New cards

ArrayList in Java Example

import java.util.ArrayList;

ArrayList<String> names = new ArrayList<String>();
names.add("alice");
names.add("bob");
names.add("eve");
for(String person : names) {
   System.out.println(person);
}

10
New cards

ArrayList are…

●Linear data structure which can hold data of the same type

●As items are added it’s grows to the necessary size

●As items are removed it can shrink to the correct size

●Like arrays, they are indexed from position 0

●Much like a list in Python

11
New cards

Creating an ArrayList in Java

import java.util.ArrayList;

ArrayList<String> myStr = new ArrayList<String>();
ArrayList<Integer> myNum = new ArrayList<Integer>();

ArrayList Notes:

  • You specify the type you will store in the ArrayList in <>’s

  • ArrayList can only hold items of a single type.

  • ArrayLists must be of OBJECTS.  If you want an arraylist of primitive types you must use the object wrapper (int -> Integer,  double -> Double etc).

12
New cards

ArrayList Methods

Note: they start with a size of 10 and grow as needed

○.add()

○.remove(index to remove)

○.remove(object to remove)

○.size() //returns how many items in the ArrayList

○.get() //returns object at index

○.clear() //clears the ArrayList

○.contains() //Searches if an item is in the list

○.isEmpty() //returns true/false

○.set(index, new) //replaces the item at position index

13
New cards

ArrayList Methods Example:

import java.util.*;

class Main {
  public static void main(String[] args) {
    ArrayList<Integer> myList = new ArrayList<Integer>();
    for (int i = 0; i < 5; i++) {
      myList.add(i);
    }
    for (int x : myList) {
      System.out.println(x);
    }
  }
}

14
New cards

Arrays

●Arrays are very similar to ArrayLists, but are more limited.

○Cannot be sliced

○Can only hold a single data type

○Cannot change size once created.

●Think of them as a liner collection of one data type

●What is the benefit of Arrays?

○They are guaranteed to occupy the amount of space you allocated when you created them.

■More on this later.

○They can be multidimensional.

15
New cards

Reading and writing “to” arrays

●Getting information from an array:

  variable = name[position]; // reads value at position in name

●Storing information in an array:

  name[position] = value;  // stores value at position in name

●Note arrays are indexed from 0

○This is probably one of the most common mistakes when using arrays

○If you create an array of size 10, it has cells 0-9, NOT 1-10.

16
New cards

Alternative syntax to create and initialize Arrays

double[] grades = new double[] {92.8,78.4,44.6};
double sum=0;
for(int i=0;i<grades.length;i++) {
    sum+=grades[i];
}
double average=sum/grades.length;
System.out.println("Average: "+average);

17
New cards

Resizing array?

  • WRONG:

create new one and copy over data

  • HOWEVER:

We can determine the size of an array by using .length

18
New cards

Searching through Arrays:

  • If you want to check if a particular element is inside an array, you’ll need to do it manually.

  • There is no “in” keyword for a “if element in array:” statement.

for(int i =0; i< array.length; i++){
	if(array[i] == targetNumber){
		System.out.println("Found at position " + i);
break;
	}
}

19
New cards

Printing arrays

  • simply using System.out.println(array); only prints out the memory representation

  • if you want to print out array contents you need to →

int [] array = {1,2,3,4,5};
String output = "[";

for(int i=0;i<array.length;i++){
	output+= array[i];
	if(i+1 != array.length){
		output+= ", "
	}
}
output += "]";
System.out.println(output);

20
New cards

Comparing Arrays

  • simply using == operator WONT work

if(array2.length != array.length){
	System.out.println("Arrays are not the same"); 
}
else{
	boolean equals = true;
	for(int i=0;..;...){
		if (array2[i] != array[i]){
			equals = false;
		}
	}
}

21
New cards

Slicing arrays:

If you want to slice an array, you’ll do it manually

The examples are equivalent:

<p>If you want to slice an array, you’ll do it manually</p><p>The examples are equivalent:</p><p></p>
22
New cards

Because the Array Class is an object, you can use some of Java’s built in “object” methods

knowt flashcard image
23
New cards

Multi-Dimensional Array of Integers

int[][] myIntArray = new int[10][10];

  • This produces a 2D array with 100 cells.

    • You can think of it as an array of arrays.

  • The first number represents the row

  • The second number represents the column

  • You access the last cell as:

    • myIntArray[9][9];

You can have 3D, 4D arrays...

24
New cards

Working with multidimensional arrays

  • ●This creates a 3x3 array

    ●Notice you’ll have 2 loops to iterate across the structure, one for rows, one for columns.

<ul><li><p><span>●This creates a 3x3 array</span></p><p><span>●Notice you’ll have 2 loops to iterate across the structure, one for rows, one for columns.</span></p></li></ul><p></p>