Recursion
Recursion
Map;
import org.apache.commons.lang.ArrayUtils;
public class Main {
public static void main(String[] args) {
String[][] countries = { { "United States", "New York" }, { "United Kingdom"
, "London" },
{ "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris"
} };
Map countryCapitals = ArrayUtils.toMap(countries);
System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));
System.out.println("Capital of France is " + countryCapitals.get("France"));
}
}
==============================================
Java String array
public class JavaStringArrayTests2
{
private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};
// our constructor; print out the String array here
public JavaStringArrayTests2()
{
// new `for` loop
for (String s: toppings)
{
System.out.println(s);
}
}
// main kicks everything off.
// create a new instance of our class here.
public static void main(String[] args)
{
new JavaStringArrayTests2();
}
}
=============================================================
Given two sorted arrays, merge them such that the elements are not repeated
Eg 1: Input:
Array 1: 2,4,5,6,7,9,10,13
Array 2: 2,3,4,5,6,7,8,9,11,15
Output:
Merged array: 2,3,4,5,6,7,8,9,10,11,13,15
import java.util.Arrays;
public class Two{
public static void main(String args[]) throws Exception {
int a[] = {10, 20, 30};
int b[]= {6,9, 14, 11};
int res[]=new int[a.length+b.length];
System.arraycopy(a,0, res, 0, a.length);
System.arraycopy(b,0,res,a.length, b.length);
Arrays.sort(res);
System.out.println(Arrays.toString(res));
} }
===============================================================
4. Find if a String2 is substring of String1. If it is, return the index of the
first occurrence. else return -1.
Eg 1:Input:
String 1: test123string
String 2: 123
Output: 4
Eg 2: Input:
String 1: testing12
String 2: 1234
Output: -1
String Str = new String("test123string");
String SubStr1 = new String("123");
String SubStr2 = new String("Sutorials");
/* System.out.print("Found Index :" );
System.out.println(Str.indexOf( 't' ));
System.out.print("Found Index :" );
System.out.println(Str.indexOf( 'o', 5 ));*/
System.out.print("Found Index :" );
System.out.println( Str.indexOf( SubStr1 ));
/* System.out.print("Found Index :" );
System.out.println( Str.indexOf( SubStr1, 15 ));
System.out.print("Found Index :" );
System.out.println(Str.indexOf( SubStr2 )); */
=========================================================================
http://examples.javacodegeeks.com/core-java/lang/string/java-string-reverse-exam
ple/
================================================================================
====
================================================================================
=============
String str = "Java";
String reverseStr = reverseRecurive(str);
System.out.println("Normal String is : " + str + " \nReverse String is : "
+reverseStr);
}
if (str.length() <= 1) {
return str;
if (str.length() <= 1) {
return str;
}
g is : "+reverseStr);
}
}
}