You are probably being recorded, perhaps even transcribed.
Getting started (this will be our normal start-of-class sequence)
For the future: I’m happy to reserve a (somewhat random) seat at the front of the classroom for those who need a front seat as an adjustment or accommodation. Just let me know.
Approximate overview
Academic/Scholarly
Cultural
Peer
Wellness
Misc
Here’s a sample list of methods for simple expandable arrays of strings.
void set(int index, String str); // Set the value at the given index to `str`.
String get(int index); // Returns the String at the given index.
int getLength(); // Returns the length of the array.
void add(String str); // Adds `str` to the end of the array.
boolean isFull(); // Determines if the array is full.
void remove(int index); // Removes the value at the given index.
void replace(int index, String str); // Replace the value at the given index.
Note: The object that’s doing the work (and storing the data) is implicit. We’ll write something like
myArray.set(5, "hello");
What method do we use to create a new expandable array of strings?
In Java, these will always be named the same thing as our class and implicitly return a value in the class.
When we call them, we write the word
newbefore them.
new ExpandableStringArray(int initialCapacity)- Create a new expandable array that is guaranteed to hold at leastinitialCapacityvalues.
new ExpandableStringArray()- Create a new expandable array. The client shouldn’t worry about trivia like how big it initially is.
When does the array expand?
We expand the expandable array as soon as it fills. (We’ll know that it’s filled because we’ve kept track of the capacity.)
We expand the expandable array as soon as we discover it needs more space. **
In what procedures might we discover that we need more space?
addToEnd(only if the length is equal to the capacity)
set. If we discover that the index is beyond the capacity, we will have to expand.
What is the “length” of an array / the “end” of the array? Is that where the value with greatest index is stored?
The
lengthis 1 + the last index we’ve used.
The
capacityis the amount of storage we have.
The
endis similar to the length.
How much should we expand when it comes time to expand? (Welcome to a stupid question that you should know for interviews.)
For
set, it must be at least enough thatcapacity > index.
One option
max(index+1, capacity+1)
- We worry about what happens if we call
addToEndagain and again and again. We’ll need to copy all of the data over again and again and again.
- If we add n+1 things, we will copy 1 + 2 + 3 + 4 + … n = $n(n+1)/2$. That’s a lot of data to copy.
Another option. Whenever we need more space, we double the capacity.
Suppose we add n+1 things again and start with room for 1. 1 + 2 + 4 + 8 + 16 + 32 + … + n
Suppose n = 2^k. Can we express that sum in terms of 2^k or k or n.
Let’s try some math.
1 + 2 = 3 = 2^2 - 1
1 + 2 + 4 = 7 = 2^3 - 1
1 + 2 + 4 + 8 = 15 = 2^4 - 1
1 + 2 + 4 + 8 + 16 = 31 = 2^5 - 1
1 + 2 + 4 + 8 + 16 + 32 = 63 = 2^6 - 1
Oooh! There’s a pattern.
That sum is 2^(k+1) - 1 is approximately 2*n to add n things. So it’s only about 2 copies per things we add. (“amortized”)
What happens if we call get without first having set a value at
the given location?
Return
nullto indicate that there’s nothing there.
Issue some other kind of error.
DO NOT PRINT ERROR MESSAGES!
Are there restrictions on what values we can use as indices for set
or get?
getbetween 0 andlength-1 OR the same value as set; in those cases, we return null.
setbetween 0 and whateverthemaxcapacityofarraysisinourlanguage
When will an expandable array be full?
What does it mean to remove a value from an array?
It’s the same as setting the index to null. (Fast)
It requires shifting everything in the array. (Slow)
Yes, it’s time for LIA
An important student question (more coming later).
I’m confused that it says that arrays are a fixed size in Java, but then it says we can still expand the array. If the array is a fixed size, then how do we expand it?
typedef struct expandableArrayOfStrings {
char **contents; // Pointer to an array
int capacity; // The number of values we can store in contents.
} Strings;
// Expanding the array
char **oldContents = contents;
contents = (char **) malloc (newsize * sizeof(char *));
// Copy stuff
Where are the double-dagger problems?
They were at the end of the arrays reading.
Why are you making us do this?
We are progressing toward being the people who implement data structures rather than just use them.
But we are also better users if we understand the likely implementations and their effects on running time.
Do the people who implement these kinds of things do so in Java, C, or Scheme, or …?
We might implement expandable arrays in any language that lacks them by default and has fixed-length arrays.
Throughout the semester, we’ll be considering whether we want to implement ADTs (data structures) using arrays (“chunks of memory”) or as linked nodes.
What’s “linear” vs. “constant time”?
Constant time: The number of steps to complete the operation is independent of the size of the structure.
Linear time: The number of steps to complete the operation varies directly with the number of things in the structure (the size of the structure).
I think I am not completely clear on when to use “import” - For
the 1st reading question, I thought I ought to include it, but
VSCode said it was never used, and the program did run fine without
it. How do I know when to write an import statement?
importintroduces shorthands, and that’s about it. If you writeimport java.io.PrintWriter, you can then writePrintWriter pen. If not, you must refer to allPrintWriterobjects using
Can you give a more detailed explanation of why you recommend using PrintWriter rather than System.out.println to print to standard output?
Sure. If we have a method and we decide that the output from the method should go to a file, rather than to the screen, it’s much easier to make the update if we’ve had a PrintWriter as the parameter.
In C, we always had to #include stdio.h. Is there something similar that we need to import at the beginning of our Java files?
Nope.
Do you have a preference if we use the buffered reader class or the scanner class?
I generally prefer
BufferedReaderobjects, but it depends on the context.
A strings mutable?
No, they are not mutable.
Can you get specific parts of strings?
Yes, you can get specific parts. Refer to the string documentation for details on
substring. (Web search for Java 17 String.)
How do you compare strings?
You write
str.compareTo(otherStr). It returns values likestrcmp. That is,
- a negative value if
strprecedesotherStr, 0- 0 if they are the same, and
- a positive value if
strshould followotherStr.
In the special Java loop for (s : args), what does s and args represent?
sis a string variable. (In general, it’s any variable.)
argsis the command-line array, likeargvin C.
Is Java a one or zero based index? I’m assuming zero based on example code but just double checking.
Zero-based. Most languages are.
I am still confused as to why Java is all object based and everything is encapsulated in a class.
Because the designers made it that way.
I want general guidelines to understand or “grok” Java transitioning from C to Java.
I think you’ll figure it out as we go.
Where is Java used in the world today?
Android phones! Lots of now-legacy software.