I am trying to all possible child domains from a given host. I have written following code. It works but my only worry is performance.
Is it required to optimize this code further:
import java.util.Arrays;
import java.util.Collections;
public class DemoExtractHostArray {
public static String[] createHostArray(String host) {
String[] stringArr = host.split("\\.");
String[] hostArray = new String[stringArr.length];
int hostIndex = 0;
for(int index = stringArr.length-1; index>=0;index--){
if(hostIndex==0){
hostArray[hostIndex] = stringArr[index];
}
else{
hostArray[hostIndex] = stringArr[index]+"."+hostArray[hostIndex-1];
}
hostIndex++;
}
Collections.reverse(Arrays.asList(hostArray));
return hostArray;
}
public static void main(String a[]){
for(String s: createHostArray("a.b.c.d.e.f")){
System.out.println(s);
}
}
}
Output:
a.b.c.d.e.f
b.c.d.e.f
c.d.e.f
d.e.f
e.f
f