Arrays & String Handling Flashcards
Create array of 12 integers
int month_days[] = new int[12];
or
int month_days[] = {31,28,25,43,23};
Can you have multidimensional array where the sizes of second dimension are unequal?
yes, it’s an array of arrays, so you can define the vertical dimension per each row.
T/F: Java implements strings as an object type String, whereas other languages implement them as character arrays?
True
T/F: String objects cannot be changed.
True, you cannot change the characters of a String.
Each time you create an altered version, a new String object is created.
How do you create a modifiable String?
use StringBuffer or StringBuilder
Create array of characters
Create a string from 3rd letter that lasts 3 characters
char chars[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’};
String s = new String(chars, 2,3);
Explain how string concatenation works with other data types
int age = 34
String s = “He is “ + age + “years old”
int value of age is automatically converted to string representation within the String object
Explain what happens what you do println of a newly created class
Box b = new Box(2,3,4)
System.out.println(b)
automatically calls b.toString() method.
However, this is seldom sufficient and you likely want to override toString() in the new class to define it how you like.
Explain:
String s1 = “Hello”
String s2 = new String(s1)
System.out.println(s1.equals(s2))
System.out.println(s1==s2)
Output:
true
false
equals method compares the characters within the String object.
== checks if references the same instance
String arr[] = {“a”, “asdf”, “as”}
Return the length of arr
arr.length
which is 3
What does compareTo do to Strings? What are the output values?
String a = “AAAAAAA”
String b = “BBBBBBB”
Returns if before or after in the dictionary
<0 is earlier
>0 is later
0 is equal
What does indexOf do?
Finds first occurrence of that character in the string
explain String method replace()
replaces a character or character sequence with another
explain String method trim()
removes leading and trailing whitespace
what does valueOf() do?
returns the human-readable String value of that input (e.g. integer to string)
What method would you use to concatenate strings with a delimiter between?
join()
String a = String.join(“, “, “alpha”, “beta”, “gamma”)
Will StringBuffer automatically grow to fit the size object you need?
yes
What does it mean an array is dynamically allocated?
Just declaring an array does not create it.
You need to allocate memory space by defining it’s size.
T/F: an array is an object?
True
T/F: When creating an array, you need to define the size of all dimensions, but you can leave the last dimension open and flexible
True
int array[][] = new int[5][]
What does it mean that arrayA = arrayB copies the reference to arrayB and not the actual values?
When copying or comparing arrays, it uses references not the underlying data.
It means arrayA has pointers to arrayB, so if you update arrayB it will also update in arrayA.
T/F: Strings also compare the references not the underlying content?
True. I guess this is how all objects work.
Could have one object ID = 9: “cat”
and another new String(“cat”) at ID=10
Same underlying, but not same in ==
To make it compare the underlying use .equals method:
a.equals(b)
Write for loop that sums all elements in an array
for (int tempint : array)
…
When camparing strings, explain difference between == and equals() method
== asks if they refer to the same object
equals() asks if contents of the strings are the same
What are command line arguments?
passed to the program at run-time, enter in command line after executing a program