នេះ​ជា​ប្លុក​សាក​ល្បង​របស់​ ភូមិខ្មែរ JAVA !!! បើ​មាន​បញ្ហា ឬ​ផ្ដល់​មតិ​រិះ​គន់​ក្នុង​ន័យ​ស្ថាបនា​ សូម​ទំនាក់ទំនង​មក​កាន់​ខ្ញុំ​បាទ​ផ្ទាល់​ តាម​រយៈ E-mail: peter.cambodia007@gmail.com , Facebook: Peter Khemara ឬតាមរយះប្រព័ន្ទ​ទូរស័ព្ទលេខ៖ ០៧៨ ៥៥ ១២ ៨៨ សូមអរគុណ!!!

Friday, November 18, 2011

Array


 Q.        What is array?
A.        Array គឺជាបណ្តុំ Variables ដែលមានឈ្មោះដូចគ្នា និង Datatype ដូចគ្នា ហើយពួកវាខុស គ្នាដោយ Index  
1.      One dimensional array
One dimensional array គឺសំដៅលើ Array មួយវិមាត្រ។
          Datatype         arrayName[ ] = new                Datatype[elements];

ឧទាហរណ៍៖
         
a[0]
a[1]
a[2]
a[3]
a[4]

ដើម្បីបញ្ជាក់ថា a ជា array លុះត្រាតែ a មានឃ្នាប​ [ ]
ចំណាំ៖
          ក្នុងភាសា Java យើងអាចបង្កើត array n ធាតុបាន ដោយសារតែចំនួន elements របស់វា យើងអាចប្រើ Variable ជំនួសបាន។
ឧទាហរណ៍៖
          int n;
n=cin.nextInt();
int a[]=new int[n];

*      import java.util.Scanner;
public class InputArrayN{
public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(“Enter n = ”);
n=cin.nextInt();
int a[]=new int[n];
System.out.println(“Now, you can enter ”+n+“ values”);
for(int i=0; i<n; i++){
System.out.print(“Enter a[“+i+”] = ”);
a[i] = cin.nextInt();
}
System.out.println(“Now, you can display ”+n+“ values”);
for(int i=0; i<n; i++)
            System.out.println(“a[“+i+”] = ”+a[i]);
}
}

*      import java.util.Scanner;
public class Max{
public static void main(String []arg){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(“Enter number of elements = ”);
n=cin.nextInt();
int a[]=new int[n];
System.out.println(“Now, you can enter ”+n+“ values”);
for(int i=0; i<n; i++){
System.out.println(“Enter a[“+i+”] = ”);
a[i]=cin.nextInt();
}
int max;
max=a[0];
for(int i=1; i<n; i++)
if(max<a[i])
            max=a[i];
System.out.println(“Maximum is ”+max);
}
}

សំគាល់៖
ក្នុងភាសា Java ត្រូវប្រកាស (Declare) ប្រភេទទិន្នន័យ (Datatype) ឡើងវិញ ព្រោះ វាត្រូវ Run នៅក្នុង for នេះសំរាប់ execute នៅក្នុង for

*      import java.util.Scanner;
public class Main{
public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(“Enter number of elements = ”);
N=cin.nextInt();
int a[]=new int[n];
System.out.println(“Now, you can enter ”+n+ “ values”);
for(int i=0; i<n; i++){
System.out.print(“Enter a[“+i+”] = ”);
a[i]=cin.nextInt();
}
for(int i=0; i<n-1; i++)
for(int j=1+i; j<n; j++)
if(a[i]>a[j]){
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(int i=0; i<n; i++)
System.out.println(“[“+i+”] = ”+a[i]);
}
}

*      import java.util.Scanner;
public class Main{
public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(“Enter number of elements = ”);
n=cin.nextInt();
int a[]=new int[n];
System.out.println(“Now, you can enter ”+n+ “values”);
for(int i=0; i<n; i++){
System.out.println(“Enter a[“+i+”] = ”);
a[i]=cin.nextInt();
}
int Search;
System.out.print(“Enter number you need to Search = ”);
Search=cin.nextInt();
System.out.print(“Index”);
boolean b=false;
for(int i=0; i<n; i++)
if(a[i]==Search){
b=true;
System.out.print(+i“,”);
}
if(b==true)
System.out.print(“Found in array.”);
else
System.out.print(“Not found in array.”);
}
}

*      import java.util.Scanner;
public class Main{
public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(“Enter number of elements = ”);
                                    n=cin.nextInt();
int a[]=new int[n];
System.out.println(“Now, you can enter ”+n+ “ values.”);
for(int i=0; i<n; i++){
if(i==0){
System.out.println(“Enter a[“+i+”] = ”);
a[i]=cin.nextInt();
}else{
System.out.println(“Enter a[“+i+”] = ”);
boolean b=false;
int value;
value=cin.nextInt();
for(int j=0; j<i; j++)
if(a[j]==value){
System.out.println(“Number already exist.”);
b=true;
break;
}
if(b==false)
a[i]=value;
}
}
System.out.println(“Now, you can display ”+n+ “ values”);
for(int i=0; i<n; i++)
            System.out.println(“a[“+i+”+a[i]] ”);
}
}

*      d

2.      Multidimensional Array
សំដៅលើ Array ច្រើនវិមាត្រ។ ជាទូទៅ  គេច្រើនប្រើត្រឹមតែ ២វិមាត្រ។

FORM:
            DataType        ArrayName[][]=new               DataType[element1][element2];

ឧទាហរណ៍៖
                                       [2]=Row    [3]=Column                           
           int a[][]=new   int[2][3];

a[0][0]
a[0][1]
a[0][2]
a[1][0]
a[1][1]
a[1][2]


import java.util.Scanner;
public class InputArray2{
            public static void main(String arg[]){
                        Scanner cin=new Scanner(System.in);
                        int r, c;
System.out.print(“Enter rows = ”);
r=cin.nextInt();
System.out.print(“Enter columns = ”);
c=cin.nextInt();
int a[][]=new int[r][c];
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
System.out.println(“Enter a[“+i+”][“+j+”] = ”);
a[i][j]=cin.nextInt();
}
System.out.println();
}
System.out.println(“Now, you can display “+r+” Rows “+c+” Columns”);
for(int i=0; i<r; i++){
for(int j=0; j<c; j++)
            System.out.println(a[i][j] + “\t”);
}
System.out.println();
            }
}

import java.util.Scanner;
public class AddElementArray{
            public static void main(String arg[]){
Scanner cin=new Scanner(System.in);
int n;
System.out.print(“Enter n = ”);
                        n=cin.nextInt();
int a[]=new int[n];
for(int i=0; i<n; i++){
System.out.println(“Enter a[“+i+”] = ”);
a[i]=cin.nextInt();
}
int temp[];
temp=a;
int m;
System.out.print(“Enter m = ”);
m=cin.nextInt();
a=new int[n+m];
for(int i=0; i<n; i++)
a[i]=temp[i];
System.out.println(“Please, enter data for new elements.”);
for(int i=0; i<n+m; i++){
System.out.print(“Enter a[“+i+”] = ”);
a[i]=cin.nextInt();
}
for(int i=0; i<n+m; i++)
            System.out.println(“a[“+i+”] = ”+a[i]);
}
}





0 comments:

Post a Comment

Share

Twitter Google+ Facebook Digg Stumbleupon Favorites More