Arrays Flashcards
Is Array an ordered list or unordered list?
Ordered
Can an array contain duplicates?
Yes
int[] arrays= new int[0];
Integer[] arrays = new Integer[0];
is arrays primitive?
No int is the primitive data type but all arrays are objects/
What are the valid datatype of array
a pic
* primitive data type
* interface
* abstract class
* concrete class
Which of the following will not compile?
public void a(String …name)
a(“Ala”);
a(new String[] {“Bret”});
Vargs arguement can take either an array or inidividual value
Which of the following will not compile?
public void a(String [] name)
a(“Ala”);
a(new String[] {“Bret”});
arrays only accepts array values not individual value.
Can an array datatype be null?
NO
Which of the following are legal and illegal?
String lion[] = new String[] {“lion”};
String tiger[] = new String[1]{“tiger”} ;
String bear[] = new String[] {};
String whale[] = new String[0]{};
Legal are lion and bear.
If you declare the size, you are not allowed {};
Can you do the following?
int intArray[2];
No it has to be the following :
int intArray[] = new int[4];
Can you do the following?
int array = new int[];
intarray[2] = new int;
does not compile, you need the size
Can you do the following?
intarray = new int[2.3];
won’t compile
Create an array with int datatype and of size 3.
int [] arrayname = new int [3];
Create an array and initialize with 3,4,5
````
int arrayname [] = {2,3,4}; //short cut
int [] numbers = new int [] {2,3,4}; //redundant
````
int [] helloworld = {1,2,3};
This type of approach is called what?
Anonymous array. You dont specify the size or the type.
int[] ids, types
creates two reference variable of type int[]
int ids[], type
one array and one int
List the five ways of declaring an array
int [] arrayName;
int[] arrayName;
int arrayName[];
int arrayName [];
~~~
int [] number = new int[4];
int number[];
int [] numbers, codes, scores;
int number[], codes, scores;
int number [], codes [], scores[]
````
What does it output? why?
````
String [] bugs = {“a”, “b”, “c”};
String [] copy = bugs;
System.out.println(bugs.equals(copy));
````
true. referenced to the same object.
String[] bugs = {“aa”, “mo”, “c”};
String[] z = {“aa”, “mo”, “c”};
System.out.println(bugs[0].equals(z[0]));
true
String[] birds = new String[6];
System.out.println(birds.length);
6 bc there are 6 slots
How to find the size of an array?
length.
not length();
What do you need to use Arrays?
java.util.Arrays;
What can you use to sort the Array?
import java.utils.Arrays;
Arrays.sort(arrayName);
If you have String what sorts first?
- Numbers (so 10 > 9)
- Uppercases
- lowercases
What can you use to search within a array?
Java’s inbuild Arrays.binarchSearch();
However it needs to be sorted before you use them
Binary search rules.
What happens if it is found?
What happens if it is not found?
What happens if it unsorted array?
1.index of match
2. negative values
3. result is not predictible