Question
Exercise 05 1. If I wanted to print the first element in an array, how would I do it? 2. If I wanted to iterate
Exercise 05
1. If I wanted to print the first element in an array, how would I do it?
2. If I wanted to iterate through a basic Java array, how would I do it? Would I use the ".length" property or the ".size()" method?
3. If I wanted to iterate through Java's ArrayList, how would I do it? Would I use the ".length" property or the ".size()" method?
4. True or False: I would use Java's basic array when I know the size of the data or the array will not change, and I should use Java's ArrayList when I need a dynamic array and I don't know how much data may or may not be added.
5. What will happen if I try to access an element in a basic array at the index of -1 like so:
int[] array = {1, 2, 3, 4, 5}; inttest = array[-1];
6. What will the following code print?
int[] array = {2, 4, 6, 8, 10};
int test = array[5];
System.out.println(test);
7. The code below prints out something like this:
[I@626b2d4a
[1, 2, 3, 4, 5]
Why does printing a basic Java array print something that seems random while Java's ArrayList prints the data in the array in a readable way?
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
ArrayList arrayList = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(array);
System.out.println(arrayList);
}
8. What is printed in the following code? (HINT: I am not passing a primitive type, but an ArrayList.
public static void main(String[] args) {
ArrayList list = new ArrayList();
System.out.println(list);
add5Numbers(list);
System.out.println(list);
}
public static void add5Numbers(ArrayList list) {
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.println(list);
}
Exercise 05
1. If I wanted to print the first element in an array, how would I do it?
2. If I wanted to iterate through a basic Java array, how would I do it? Would I use the ".length" property or the ".size()" method?
3. If I wanted to iterate through Java's ArrayList, how would I do it? Would I use the ".length" property or the ".size()" method?
4. True or False: I would use Java's basic array when I know the size of the data or the array will not change, and I should use Java's ArrayList when I need a dynamic array and I don't know how much data may or may not be added.
5. What will happen if I try to access an element in a basic array at the index of -1 like so:
int[] array = {1, 2, 3, 4, 5}; inttest = array[-1];
6. What will the following code print?
int[] array = {2, 4, 6, 8, 10};
int test = array[5];
System.out.println(test);
7. The code below prints out something like this:
[I@626b2d4a
[1, 2, 3, 4, 5]
Why does printing a basic Java array print something that seems random while Java's ArrayList prints the data in the array in a readable way?
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
ArrayList arrayList = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(array);
System.out.println(arrayList);
}
8. What is printed in the following code? (HINT: I am not passing a primitive type, but an ArrayList.
public static void main(String[] args) {
ArrayList list = new ArrayList();
System.out.println(list);
add5Numbers(list);
System.out.println(list);
}
public static void add5Numbers(ArrayList list) {
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.println(list);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started