Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Tuesday, April 28, 2009

Java Example : How To Swap Integer and String Variables Without Using a Temporary Variable

Hi guys,

Swapping two variable has always been a classic learning example for beginners, well for a change in the example below we would be learning to swap two variables (String or Integer) without using a temporary variable.

/*

  *   @(#)Swap.java       1.1   28/04/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;





/**

  *   A   class   that   demonstrates   how   to   swap  

  *   two   strings   or   two   integers   without

  *   using   a   temporary   variable

  *  

  *   @author   lazycoder

  *  

  */


public   class   Swap   {



      

       /**

         *   @param   args

         */


       public   static   void   main(String[]   args)   {

              //Swapping   two   String   without   using   temp   variable.

              String   firstString   =   "Lazy";

              String   secondString   =   "LazyCoder";

              System.out.println("firstString   =   "   +   firstString);

              System.out.println("secondString   =   "   +   secondString);

              System.out.println("Swapping");

              firstString   =   firstString   +   secondString;

              secondString   =   firstString.substring(0,   firstString.length()

                            -   secondString.length());

              firstString   =   firstString.substring(secondString.length());

              System.out.println("firstString   =   "   +   firstString);

              System.out.println("secondString   =   "   +   secondString);

             

              //Swapping   two   Integers   without   using   temp   variable.

              int   firstInt   =   111111111;

              int   secondInt   =   999999999;

              System.out.println("firstInt   =   "   +   firstInt);

              System.out.println("secondInt   =   "   +   secondInt);

              System.out.println("Swapping");

              /*

                *   Using   exclusive-or   (XOR)   to   avoid  

                *   stack   overflow/underflow

                */


              firstInt   =   firstInt   ^   secondInt;

              secondInt   =   firstInt   ^   secondInt;

              firstInt   =   firstInt   ^   secondInt;

              System.out.println("firstInt   =   "   +   firstInt);

              System.out.println("secondInt   =   "   +   secondInt);

       }

}



Thats all for now, check back for more java examples.

Lazy Coder
signing off for now...

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Monday, April 27, 2009

Adding two Object arrays or Collections in Java

Hi guys,
I was feeling a little bit bored so thought of posting a java example on my blog. Starting with a basic one for now, many of the java newbies have faced problems while trying to combine two Object arrays or collections in java. We will learn just how to do that in this post.

PS: I have used the extended for loop in the example below and so it requires JDK 5 or above to run.

The code below will not hold duplicates in the resultant; if you want to hold duplicates as well then define alist as ArrayList instead of a HashSet.

/*

  *   @(#)CombiningCollections.java       1.1   27/04/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;



import   java.util.Arrays;

import   java.util.HashSet;



/**

  *   @author   lazycoder

  *  

  */


public   class   CombiningCollections   {



       /**

         *   A   method   which   takes   two   collections

         *   as   parameters   and   returns   a   resultant

         *  

         *   @param   collection1

         *   @param   collection2

         *   @return

         */


       public   Object[]   combineCollections(Object[]   collection1,

                     Object[]   collection2)   {

              HashSet   alist   =   new   HashSet();

              for   (Object   obj   :   collection1)

                     alist.add(obj);

              for   (Object   obj   :   collection2)

                     alist.add(obj);

              Object[]   collection   =   alist.toArray();

              /*

                *   Used   to   sort   the   elements   in   ascending   order

                *   all   elements   in   the   array   must   be   mutually

                *   comparable(that   is,   e1.compareTo(e2)   must   not  

                *   throw   aClassCastException   for   any   elements  

                *   e1   and   e2   in   the   array).

                */


              Arrays.sort(collection);

              return   collection;

       }



       /**

         *   @param   args

         */


       public   static   void   main(String[]   args)   {

              Object[]   collection1   =   {   "A",   "C",   "1",   "D"   };

              Object[]   collection2   =   {   "Bits",   "Nibble",   "Bank"   };

              CombiningCollections   combine   =  

                     new   CombiningCollections();

              Object[]   collection   =  

                     combine.combineCollections(collection1,

                            collection2);

              for   (Object   obj   :   collection)

                     System.out.println(obj);

       }

}



Hope you guys will find this helpful.

Lazy Coder
Signing off for now....

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl