Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

For Finding Length of Array.: Java Cheat Sheet

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Java Cheat Sheet

Taking input: import java.util.Scanner;


Scanner sc = new Scanner(System.in);
sc.next().charAt(0); for taking char as input, nextInt(), nextLine(), nextFloat(),
nextDouble(), nextLong(), nextBoolean(), next() reads a word from the user.
java.util.Arrays;
Declare an array
String[] arr0 = new String[5];
String[] arr5 = {"a","b","c", "d", "e"};
String[] arr6 = new String[]{"a","b","c","d","e"};
i = arr.length; for finding length of array.
Arrays.sort(arr); for sorting array
index = Arrays.binarySearch(arr, element to be searched);
boolean ans = Arrays.equals(arr1, arr2);
for (int i : arr) {
System.out.println("Number = " + i);
}
String[] words = input.split(" "); input is a string and we use split function to split that
string from any character and store it into an array.
input.length() for finding length of string.
StringBuilder sb = new StringBuilder(word[i]);
StringBuilder reverseStr = sb.reverse(); to reverse a string
Print an array in Java
int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);
System.out.println(intArray); // [I@7150bd4d print directly will print reference
value.
System.out.println(intArrayString); // [1, 2, 3, 4, 5]
Create an ArrayList from an array
String[] Array1 = { "a", "b", "c", "d", "e" };
ArrayList<String> l1 = new ArrayList<String>(Arrays.asList(Array1));
System.out.println(l1); // [a, b, c, d, e]
Convert an ArrayList to an array
String[] stringArr1 = { "a", "b", "c", "d", "e" };
ArrayList<String> l1 = new ArrayList<String>(Arrays.asList(stringArr1));
String[] stringArr2 = new String[l1.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
Convert an array to a set
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);
ArrayList
java.util.Collections;
ArrayList<Integer> l1 = new ArrayList<>();
i = l1.size()
l1.isEmpty();
l1.add(index, element); this will insert the element at the particular index(it will not
overwrite the value at that index) if you only give element without index then it will
append that element at the end of ArrayList.
System.out.println(l1);
for(String i : l1){
System.out.println(i); }
l1.get(index_value); to get the value from particular index
l1.set(index, value); this will overwrite the value at that index
Collections.sort(l1); this will sort a list of words, characters, integers
Collections.reverse(l1); this will reverse the arraylist
Collections.rotate(l1, 2); this will rotate the arraylist and bring index 2 element at
index 0;
l1.addAll(index, l2); if index is not given then it will append all the elements of l2 at
last of l1.
l1.remove(element/element_at_index);
l1.removeAll(l2); it will remove all the elements of l2 present in l1
l1.removeIf(str -> str.contains(element));
l1.retainAll(l2); this will keep the elements of l2 present in l1 and those elements
present in l1 only will be deleted
int i = l1.indexOf(element); Returns the index of the first occurrence of the specified
element in this list, or -1
int i = l1.lastIndexOf(); Returns the index of the last occurrence of the specified
element in this list, or -1
HashMap
HashMap<Integer,String> map=new HashMap<Integer,String>();
map.put(key, value);
for(Map.Entry m : map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue()); }
Map.Entry interface contains the getKey() and getValue() methods. But, we should call the
entrySet() method of Map interface to get the instance of Map.Entry.
map.putAll(map2);
map.remove(100); key based removal of pairs
HashSet
HashSet<String> set=new HashSet<>();
set.add("One");
while(i.hasNext()){
System.out.println(i.next()); }
set.remove("Ravi");
set.addAll(set1);
=============================================================
=============
import java.util.*;
import java.io.*;
public class Main{
public static boolean areDistinct(Integer arr[]){
Set<Integer> s = new HashSet<Integer>(Arrays.asList(arr));
return (s.size() == arr.length);
}
static int getMin(Integer arr[], int n){
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.min(res, arr[i]);
return Arrays.asList(arr).indexOf(res); //to get the index from array element
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Integer[] arr1 = new Integer[n];
Integer[] arr2 = new Integer[n];
for(int i=0; i<n; i++){
int val = sc.nextInt();
arr1[i] = val;
}
for(int i=0; i<n; i++){
int val = sc.nextInt();
arr2[i] = val;
}
Integer[] arr3 = new Integer[n];
for(int i = 0; i<n; i++){
int val = arr1[i]/arr2[i];
arr3[i] = val;
}
boolean res = areDistinct(arr3);
if(res){
int l = arr3.length;
int ans = getMin(arr3, l);
System.out.println(ans);
}
else{
int l = arr3.length;
int ans = arr3[getMin(arr3, l)];
int j = 0; //comma ko sahi se print krne ke liye j ka use kiya h
for(int i=0; i<n; i++){
if(arr3[i]==ans && j==0){
System.out.print(i);
j++;
}
else if(arr3[i]==ans){
System.out.print(","+i);
}
}
}
}
}

You might also like