Friday, September 23, 2016

1d arrays

20 වන පාඩම(1d arrays)
Data store කර තියාගන්න ලේසි විදියක් මේක. වගේම පහසුවෙන් data වලට acces වෙන්නත් පුලුවන්.එකම ටයිප් එකේ data store කරගන්න නිසා array එක තුල data compare කරන්න,max,min,sort වගේ දේවල් පහසුවෙන් හොයන්න කරන්න පුලුවන්.
මුලින්ම බලමු කොහොමද array එකක් initialize කරන්නෙ කියල.
int array[]=new int[5];
මෙතනදි වමේ  [] එක int  ගාවින් ලියන්නත් පුලුවන්.
int[] array=new int[5];
මෙතනදි array type එක integer සහ size එක 5.
මේකෙන් අදහස් වෙන්නෙ array එකේ 0,1,2,3,4 වෙනකන් integer element දාන්න පුලුවන්.මතක තියාගන්න array වල index පටන්ගන්නෙ 0 ඉදන් කියල.size එක n නම් index තියනව 0 සිට n-1 වෙනතුරු. වගේම මෙතනදි හැම index එකකම තියන data value එක 0. Integer  array වල    default අගය ගන්නෙ 0.
 දැන්  index වලට  අදාලව data දෙන්න ඕනි. අපිට කැමති පිලිවෙලකට data දෙන්න පුලුවන්. වගේම නැවත නැවත data වෙනස් කරන්න පුලුවන්

public class ArrayDemo
 {
            public static void main(String[] args)
            {         
            int array[]=new int[5];
                        array[2]=15;
                        array[3]=16;
                        array[4]=17;
                        array[0]=10;
                        array[1]=11;
                       
                        System.out.println(array[0]);
                        System.out.println(array[1]);
                        System.out.println(array[2]);
                        System.out.println(array[3]);
                        System.out.println(array[4]);
                        array[0]=3;
                        array[1]=5;
                        array[3]=4;
                        array[2]=8;
                        array[4]=9;
                        System.out.println(array[0]);
                        System.out.println(array[1]);
                        System.out.println(array[2]);
                        System.out.println(array[3]);
                        System.out.println(array[4]);
            }
}
මෙතනදි data user ගෙන් read කරන්නත් පුලුවන්.
තව විදියකට Array initialize කරන්න පුලුවන්.

public class ArrayDemo2
 {
            public static void main(String[] args)
            {         
            int array[]={7,8,1,6,5};
                        System.out.println(array[0]);
                        System.out.println(array[1]);
                        System.out.println(array[2]);
                        System.out.println(array[3]);
                        System.out.println(array[4]);
                       
            }
}
මෙතනදි {} තුල අපට ඕන data ටික දැම්මම ඊට අදාල index වලට පිලිවෙලින් data allocate  වෙනව.
දැන් for-loop,while-loop බාවිත කරානම් තවත් ලේසියි නේද.
දැන් අපිට කැමති size එකක array එකක් හදල ඒකට value read කරල ප්රින්ට් කරමු.

import java.util.*;
public class ArrayDemo2
 {
            public static void main(String[] args)
            {         
            Scanner s=new Scanner(System.in);
            System.out.println("enter array size");
            int n=s.nextInt();
            int[] array=new int[n];
                        for(int i=0;i<n;i++)
                        {
                                    System.out.print("enter element for index"+i+": ");
                                    array[i]=s.nextInt();
                        }
                       
                        System.out.println("here the array elements ");
                        for(int i=0;i<n;i++)
                        {
                                    System.out.println("index"+i+"= "+array[i]+" ");
                        }
            }
}
දැන් බලමු අනෙක් data type වල array හදන්නෙ කොහොමද කියල.

String s[]=new String[5];
                        s[0]="one";
                        s[1]="two";
                        System.out.println(s[0]);
                        System.out.println(s[1]);
                       
                        String s2[]={"one","two"};
                        System.out.println(s2[0]);
                        System.out.println(s2[1]);
                       

String වලදිත් ඉහත ආකාර දෙකෙන්ම හදන්න පූලුවන්.දෙවනි විදියට හදන්කොට “ ” පාවිච්චි කල යුතු වෙනව.
Char වල array හදන්නෙ කොහොමද බලමු.

char ch[]={'1','5'};
            char ch2[]=new char[5];
‘ ‘  බාවිත  වෙනව මෙතනදි.පටලව ගන්නෙපා string වලදි “ “
           
           
           
            double dd[]=new double[20];
            long ll[]=new long[20];
            float ff[]=new float[20];

මතක තියාගන්න  array වල size එක fix කියල.
අපි array එකක් හැදුවම ඒකෙ size එකට වඩ වැඩියෙන් දැම්මොත් out of bound වෙනව කියල.ඒ වගේම size එකට වැඩියෙන් index access කරන්න හැදුවත්  out of bound වෙනව කියල.


Thursday, August 4, 2016

try-catch-finally

19 වන පාඩම(try-catch-finally)

ජාව වල programe රන්  සමහර වෙලාවට exception සිදුවෙනව. මෙම exception සිදු වුනාම programe එක එතනින් එහාට වැඩ කරන්නෙ නෑ.
·         හිතන්න user ගෙන් integer read කරනකොට  double value  එකක් දුන්නොත්
·         Operation කරනකොට අගයක් 0 න් බෙදුවොත්.
·         Array (ඉදිරියේ  lesson එකක්) වල size එකට වඩා වැඩි index එකක index එකකට acces වෙන්න හැදුවොත්.
ඉහත අවස්තාවල programe එක compile වෙනව නිවැරදිව.නමුත් රන් වෙනකොට තමයි මෙම exeption වෙන්නෙ.එහෙම වෙනකොට programe එක නතර නොවී ඊලගට කලයුතුදේ කරගන යන්න මෙම try-catch ප්රයෝජනවත් වෙනව.
වගෙම scanner වගෙ resources අවසානයෙ close කරන්න පුලුවන් වෙනව Exception සිදුවුන අවස්තාවෙ උනත්.
මේක හරිම ලේසියි.
කරන්න තියෙන්නෙ error එකක් වෙන්න ඉඩ තියන කොටස try block එක ඇතුලෙ ලියන එක විතරයි. try block එක ඇතුලෙ වරදක් උනොත්  programe එක නතර නොවී catch block එකට යනව ඒකෙ ඇතුලෙ තියන statement ටික execute වෙනව .finally block එක අත්යවශ් නෑ.ඒක හැමවෙලාවෙම execute වෙනව එහි ඇතුලත තියන statement.

මුලින්ම try-catch විතරක් බලමු.තේරුම්ගන්න ඒකෙ තියන පොඩි අවාසියක් පහල example එකේ.
import java.util.Scanner;
public class errors
 {
            public static void main(String[] args)
            {          try
                        {
                        Scanner s=new Scanner(System.in);
                        int j=s.nextInt();
                        System.out.println("j="+j);
                        s.close();
                        }
                        catch(Exception e)
                        {
                                    System.out.println("exception happened in the try block. exception ="+e);
                                   
                        }
            }
}
ඉන්පුට් එක integer නොදුන්නොත් එතනින් පහල try block එකෙ කිසිම statement එකක් execute වෙන්නෙ නෑ.
exception happened in the try block. exception =java.util.InputMismatchException කියල ප්රින්ට් වෙනව.මෙතන Exception කියනෙක වෙනුවට අපිට ඕන specific එක දෙන්න පුලුවන්.අපි  දන්නෙ නැත්නම් හරියටම වෙන්න යන Exception එක  මේ විදියට දෙන්න පුලුවන්.
Exception කියනෙක යොදාගන්නවනම් util package එක ඉම්පොර්ට් කරන්න ඕනි නෑ.

මේ උදාහරනයෙදි j වලට  integer අගයක්නෙ එන්න ඕනි.
InputMismatchException කියන exception එක තමයි වෙන්නෙ වෙන අගයක් දුන්නොත්.
Exception වෙනුවට InputMismatchException දෙන්න පුලුවන් අල්ලන Exception එකට .ඒකට
java.util.InputMismatchException කියන class එක import කරගන්න ඕනි.
import java.util.InputMismatchException;කියල දෙන්න පුලුවන් ක්ලාස් එකේ උඩින්ම.


මෙතනදි scanner එකත් import කරානෙ.තව exeption ආවොත් ඒවටත් import කරන්න වෙනව.ඒක එපාවෙනවනෙ.
අපි util package එකේ සේරම import කරගන්නව ලේසි වෙන්න.
import java.util.*;
public class errors
 {
            public static void main(String[] args)
            {          try
                        {
                        Scanner s=new Scanner(System.in);
                        int j=s.nextInt();
                        System.out.println("j="+j);
                        s.close();
                        }
                        catch(InputMismatchException e)
                        {
                                    System.out.println("exception happened in the try block. exception ="+e);
                                   
                        }
            }
}
ඕනම package එකක සේරම class import කරගන්න .*; use කරනව.
දැන් අගයන් දෙකක් අරගන එකක් 0 කරල අනෙක් අගය 0 බෙදන operation එක කරල බලන්න.
java.lang.ArithmeticException: / by zero
කියන ArithmeticException එක වෙනව.
ArrayIndexOutOfBoundsException වෙනව Array (ඉදිරියේ  lesson එකක්) වල size එකට වඩා වැඩි index එකක index එකකට acces වෙන්න හැදුවොත්.

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111
දැන් හිතන්න exeption එක උනාම ඊට පහල try block එකෙ ඒව execute වෙන්නැත්නම් කොහොමද  scanner s කියනෙක close කරන්නෙ.
Catch block එකට දානවද? එහෙම දාන්නනම් 
Scanner s=new Scanner(System.in);
කියන statement එක try block එකට පිටින් දාන්න වෙනව.
public static void main(String[] args)
            {         
            Scanner s=new Scanner(System.in);
            try
                        {
                       
                        int j=s.nextInt();
                        System.out.println("j="+j);
                        s.close();
                        }
                        catch(Exception e)
                        {
                                    s.close();
                                    System.out.println("exception happened in the try block. exception ="+e);
                                   
                        }
            }
මෙතනදි නිකරුනේ s.close(); එකම statement එක use කරනව.finally block එක use කරොත් එහෙම වෙන්නෙ නෑ.
import java.util.Scanner;
public class errors
 {
            public static void main(String[] args)
            {         
            Scanner s=new Scanner(System.in);
            try
                        {
                       
                        int j=s.nextInt();
                        System.out.println("j="+j);
                        }
                        catch(Exception e)
                        {
                                   
                                    System.out.println("exception happened in the try block. exception ="+e);
                                   
                        }
                        finally
                        {
                                    s.close();
                        }
            }
}

.cacth block එකට යන්නෙ try block එකේ අව්ලක් නම් විතරනෙ.ඒත් Finally block එකේ ඒව හැමවෙලාවෙම රන් වෙනව. නිසා s.close(); එක finally block එකේ ලිව්වම හරි.

තැනට ගැලපෙන විදියට try-catch-finally යොදාගන්න.සමහර අවස්තාවල finally block එක අවශ් වෙන්නෙ නෑ.

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111ජාව 7 හෝ  8 use කරනවනම් සමහර resources initialize කරන්න    යොදාගන්නව try() පහත විදියට  .මතක තියාගන්න පහල example හරි නොගියොත්ජාව version එක බලන්න,
import java.util.Scanner;
public class errors
 {
            public static void main(String[] args)
            {         
           
            try(Scanner s=new Scanner(System.in))
                        {
                       
                        int j=s.nextInt();
                        System.out.println("j="+j);
                        }
                       
            }
}
මෙතනදි s.close(); කියනෙක සිදුවෙනව.නමුත් අනෙක් exception අල්ලන්නෙ නෑ.මේ ක්රමය හරියටම තේරුම්ගන්න .මෙතනින් වෙන්නෙ බාවිත කරපු resourse එක close කරනෙක.පහල විදියට යොදාගත්තනම් හරි.
import java.util.Scanner;
public class errors
 {
            public static void main(String[] args)
            {         
           
            try(Scanner s=new Scanner(System.in))
                        {
                       
                        int j=s.nextInt();
                        System.out.println("j="+j/0);
                        }
                        catch(Exception e)
                        {
                                   
                                    System.out.println("exception happened in the try block. exception ="+e);
                                   
                        }
                       
            }
}
Nested try-catch එහෙමත් යොදාගන්න පුලුවන්.බලන්න  exception ගොඩක් අල්ලන  විදියක් ,
import java.util.*;
public class errors2
 {
            public static void main(String[] args)
            {         
           
            try(Scanner s=new Scanner(System.in))
                        {
                       
                        int j=s.nextInt();
                        System.out.println("j="+j);
                        j=j/0;
                        }
                        catch(InputMismatchException e)
                        {
                                   
                                    System.out.println("exception happened in the try block. exception ="+e);
                                   
                        }
                        catch(ArithmeticException e)
                        {
                                   
                                    System.out.println("exception happened in the try block. exception ="+e);
                                   
                        }
                        catch(Exception e)
                        {
                                   
                                    System.out.println("exception happened in the try block. exception ="+e);
                                   
                        }
                        finally
                        {
                                    System.out.println("always execute");
                        }
                       
            }
}

මන් highlight කරල තියන block එක අවසානෙට තියෙන්න ඕනි.ඒක මුලින් දැම්මොත් එහෙම error එකක් එයි.මේ example එකේ විදියට catch ඕනම   ප්රමානයක් සුදුසු විදියට යොදාගන්න පුලුවන්