Posts

Showing posts from 2014

Java program to access an URL

Hi Everyone, in this blog post I will be writing a java program to access an URL. The Java program to connect to an URL is as follows: package com.blog.techjourney; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class AccessURL { public static void main(String[] args) { String url = "http://sujithforu.blogspot.com/" ; String fakeurl = "http://thisisanincorrecttypeofurl.com/" ; System.out.println( " Connecting to existing url - " +url); getURLResponse(url); System.out.println(); System.out.println( " Connecting to fake url - " +fakeurl); getURLResponse(fakeurl); } public static void getURLResponse(String str) { URL url; URLConnection urlConnection; HttpURLConnection httpConnection = null ; try { url = new URL(str); urlConnection = url.openConnection(); urlConnection.co

Removing duplicates and ordering a list using java

Hi everyone, in this blog post, I will be writing about ' How to remove duplicates and order a list using java '. For removing duplicates in a list, we can put all the contents of the list to a set. For ordering the set, we can use an implementation of treeset. So in general, to remove duplicates and order a set, we could do by putting all the contents of the list to a treeset. Please refer my post about sets to understand more about treesets. Here is the java program to remove duplicates and order a list. package  com.blog.techjourney; import  java.util.ArrayList; import  java.util.List; import  java.util.Set; import  java.util.TreeSet; public   class  removeDuplicatesAndOrder {      public   static   void  main(String[] args) {         List  countries =  new  ArrayList ();                  countries.add( "Canada" );         countries.add( "Australia" );         countries.add( "Brazil" );         countries.add( "Canada"

Finding third largest element in an array using Java

Hi everyone, in this blog post, I am writing a Java program to find the third largest element in an array. When we are asked to write a program about finding the third largest element in an array, the trivial solution which comes to our mind is sorting the array and returning the third element from the end. But sorting an array is of time complexity O(nlogn) and   a program always requires to minimize the total time complexity and space complexity. We can do the above program in time O(n). The logic of the program is as follows: Initialize three variables as firstLargest , secondLargest and thirdLargest to Integer.MIN_VALUE Iterate the loop of elements in the array. For each number , check the following steps If firstLargest is less than the number , assign thirdLargest to secondLargest , secondLargest to firstLargest and firstLargest to the number . Else if secondLargest is less than the number and if number not equal to firstLargest , then assign thirdLargest to

Static blocks in java

Introduction Hi everyone, it has been a while since I have put a new topic. In this blog post, I will be writing about static blocks in java. Static blocks is a block of code that get executed first when the class is loaded. Static block will be the first to execute when a class is called. Please check the below example. package  com.blog.techjourney; public   class  StaticBlockExample {           static       {         System.out.println( " Inside static block" );     }      public   static   void  main(String[] args)     {         System.out.println( " Inside main method" );     }      } In the above example, the static block will get executed before the main method. The output of the above program is as follows: Inside static block Inside main method If there are more than one static block in a class, each of the static blocks get executed before the main method. The order of execution of the static block will be the same as t

Reversing of char array using java

Hi everyone, in this blog post I will writing about reversing a char array using java. This program requires a char array as its input and it will return the reversed version of that char array. The program which I have written to demonstrate reversing of char array is as follows: package  com.techjourney.methods; public   class  ReversingCharArray {           public   static   void  main(String args[])     {          char  firstArray[] =  new   char []{ 'G' , 'i' , 'l' , 'c' , 'h' , 'r' , 'i' , 's' , 't' };          char  secondArray[] =  new   char []{ 'D' , 'r' , 'a' , 'v' , 'i' , 'd' };                  System.out.println( " First array before reversing - " );         System.out.println(firstArray);                  System.out.println( " First array after reversing - " );         charReversal(firstArray);         System.o

arraycopy method in java

Hi everyone, its been a long time since I posted. So here is a new one. In this blogpost, I will writing about arraycopy() in java. Its a common interview question these days. It copies an array from specified source array, beginning at the specified position to the specified position of destination array. arraycopy() has got 5 parameters. They are: 1) src - The source array 2) srcPos - The position in source array from where the elements are copied 3) des - The destination array 4) desPos - The position in destination array where the elements copied from source array is pasted. 5) length - Number of elements to be copied from source array. Please check the below program to understand more about arraycopy(). package  com.techjourney.methods; public   class  ArrayCopyMethod {      public   static   void  main(String args[])     {          int  sourceArray[] =  new   int []{1,2,3,4,5,6};          int  destinationArray[] =  new   int []{7,8,9,10,11,12,13,14,15};