Wednesday, February 20, 2019

Lesson 2-Array, String

Array is a set of values with same data type.
Array of number
int ar[]={1,3,5,6,78,85,23};
Array of string
String ar[ ]= {"Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"  };
int array[]=new int[10];
We declare array contain 10 elements. First element is array[0]
Number of element call array size.
int size=array.length;
To print all element in array.
for(int i=0;i<ar.length;i++){           
     System.out.println(ar[i]);
                    
     }   

To print only even number, add if command.
if(ar[i]%2==0)
Run to see result.

Create array ar2 with number from 1-10
int ar2[]={1,2,3,4,5,6,7,8,9,10};
Copy these lines to print.
int sum=0;
for(int i=0;i<ar2.length;i++){
          if(ar2[i]%2==0){
          sum=sum+ar2[i];
          }             
          }   
sum=sum+ar2[i]; mean sum previous value with current value.
You can also write it like this sum+=ar2[i];
If we want to know position of number 85 in ar array, use for like this.
int index=0;
for(int i=0;i<ar.length;i++){
          if(ar[i]==85){
          index=i+1;
          }             
          }   
Value equal 6, plus 1 because i=0 mean first elements.
We want to count how many even number in array ar2.
int count=0;
     for(int i=0;i<ar2.length;i++){
          if(ar2[i]%2==0){
          count=count+1;
          }             
          }   
Declare another array.
int ar3[]={18,34,150,6,78,85,23};
We want to print max number in ar3, copy these lines.
   int max=ar3[0];
     for(int i=0;i<ar3.length;i++){     
          if(ar3[i]>max){
          max=ar3[i];
          }             
          }   
Result is 150.






Next to two dimension array, we declare like this
int array2[][]=new int [2][3];
array2[0][0]=4;
array2[0][1]=8;
array2[0][2]=6;
array2[1][0]=5;
array2[1][1]=9;
array2[1][2]=3;
Now array has 2 rows, 3 column, elements look like this
4 8 6
5 9 3
We can also declare like this
int array3[][]={{4,8,6},{5,9,3}};
Element at row 2, column 3 will be write array3[1][2], it is 3.
To print value in two dimension array, we use for like this.
for(int i=0;i<2;i++){
     for(int j=0;j<3;j++){
     System.out.println(array2[i][j]);
     }             
     }   
To understand double for loop, think like this:
First, i=0, run for below with j from 0 to 2
Next, i=1, run for with j from 0 to 2
That mean we run single for with j in i times. Here i mean number of row, i=0 is first row, i=1 is second row.
Repeat single for mean double for loop, you should understand it in practical.
Make the line System.out.println(array2[i][j]); become comment.
Next to arraylist, we declare like this.
ArrayList  array4=new ArrayList();
Or declare with data type:
ArrayList<String> mang4 = new ArrayList<String>();
To add value to arraylist, use add().
array4.add("Monday");
array4.add("Tuesday");
array4.add("Wednesday");
Arraylist has no fix size, we can add more element in any time.
To get number of element in arraylist, use int size=array4.size();
To get element, use get.
array4.get(0);
To delete an element, use array4.remove(index);
For example, to remove second element,use array4.remove(1);
To delete all, use array4.clear();
Next to string character.
We need to know about split string.
For example, we have a string like this.
String a="This is example text, we will split by comma";
We create an array by split string like this.
String a1[] = a.split(",");
System.out.println(a1[1]);

Array a1 now has two elements, “This is example text” and “we will split by comma"
We can also cut string like this
String cut = a1[0].substring(0, 7);
Result is string “This is


Blank chacracter is counted as one character.

No comments:

Post a Comment