Java Flashcards
List is Child of
Collection
How do you convert a long to a String
Long.toString()
how do you make a two-dimensional array
int [][] hi=new int[m][n];
How do you reverse a list
Collections.reverse(list)
it reverses the input for you! it
List is an interface, so you cant make a new list, who are the children?
ArrayList,
how do you compare two strings lexicographically?
public int compareTo(String str)
How to convert a character to an integer in java?
Character.getNumericValue(c);
How to convert an integer to a character in java?
int REDIX = 10;
char c = Character.forDigit( a, REDIX );
How to sort with a comparable?
Collections.sort(persons, new Comparator() { public int compare(Person person, Person person1) { int name = person.getName().compareTo(person1.getName()); if(name == 0){ return name; } return person.getAge() > person1.getAge() ? 1 : person.getAge() < person1.getAge() ? -1 : 0; } });
How do you read input in Java, only tell me 3 methods and their advantages:
1.Using Buffered Reader Class: efficient reading
2. Using Scanner Class: Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
Regular expressions can be used to find tokens.
3. Using Console Class, for not echoing password and stuff, and it’s synchronized.
How do you read from a Buffered Reader Class?
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
String vs StringBuilder vs StringBuffer in Java, when to use which?string,
If a string is going to remain constant throughout the program, then use String class object because a String object is immutable. If a string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough. If a string can change and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.
how do you concatenate with a String buffer or a String Builder?
.append();
.toString() at the end;
can you append an integer to string buffer or String Builder?
yes you can append, long, double, float, int, boolean,
String and
What is the easiest way to reverse a string in java?
using string buffer or String Builder! String s="khello"; StringBuffer sb=new StringBuffer(s); String rev=sb.reverse.toString();
How do we check strings equality?
equals
how do you get a substring of a string?
String substring(begIndex, endIndex) The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1, if the second argument is given.
How can we know a character is alphanumeric?
Character.isLetterOrDigit
get the highest and the lowest element in a treeSet?
- pollFIrst()
2. pollLast() -> highest
how to get the min and max value?
Integer.MIN_VALUE
Integer.MAX_VALUE
how do you use max or min-heap in Java?
We use PriorityQueue class to implement Heaps in Java. By default Min Heap is implemented by this class. To implement Max Heap, we use Collections.reverseOrder()
How to convert a set into an Arraylist in java?
we make a new ArrayList(set);