Posts

Array&Sparse Array

Image
  Array How to create an array? // method 1 //dataType[] name = value int [] nums ; // declare an array // method 2 //dataType name[] = value int nums []; Static initialization int [] a = { 1 , 2 , 3 } Dynamic initialization int [] a ; a = new int [ 3 ]; a [ 0 ] = 1 ; How to use an array? Array in for loop: int [] nums ; //1. declare an array nums = new int [ 3 ]; //2. create an array nums [ 0 ] = 1 ; //3. assign a value nums [ 1 ] = 2 ; nums [ 2 ] = 3 ; System . output . println ( nums [ 0 ]); //4.print a value // 1. print all of elements for ( int i = 0 ; i < nums . length ; i ++ ){   System . output . println ( nums [ i ]); } // 2. calculate the sum of an array int sum = 0 for ( int i = 0 ; i < nums . length ; i ++ ){   sum += nums [ i ]; } //3. fint the max element of array int max = nums [ 0 ]; for ( int i = 0 ; i < nums . length ; i ++ ){   if ( array [ i ] > max ){     max = array [ i ]; } } Array in f...

Practice - bubble sort

Bubble sort package Array ; ​ import java . util . Arrays ; ​ public class BubbleSort {     public static void main ( String [] args ) {         int [] nums = { 1 , 3 , 4 , 2 , 5 };         int [] result = BubbleSort ( nums );         System . out . println ( Arrays . toString ( result ));   }     public static int [] BubbleSort ( int [] array ){         int temp ;         for ( int i = 0 ; i < array . length - 1 ; i ++ ) {             for ( int j = 0 ; j < array . length - 1 - i ; j ++ ) {                 if ( array [ j ] < array [ j + 1 ]){                     temp = array [ j + 1 ];                     array [ j + 1 ] = array [ j ];   ...

Practice - find prime number

Find prime number What is prime number? Divisible only by 1 and itself package loopPrac ; ​ public class primeNumber {     //Question : find out the prime number between 101-105     public static void main ( String [] args ) {         outer : for ( int i = 101 ; i < 150 ; i ++ ){             for ( int j = 2 ; j < i / 2 ; j ++ ){ //half of the number                 if ( i % j == 0 ){ // find out the number that can divide i                     continue outer ;               }           }             System . out . print ( i + "\t" );       }   } } ​  

Scanner, while&do while, switch, break&continue

  Scanner,while&do while, switch, break&continue The difference between next() and nextLine() /* nextLine() **/ public class next_nl {     public static void main ( String [] args ) {         Scanner scanner = new Scanner ( System . in );       // String in = scanner.nextLine();     String in = scanner . next ();         System . out . println ( "you entered:" + in );         scanner . close ();   } } // output of nextLine(): //hello lil //you entered:hello lil ​ //output of next(): - while encountering space after a character, it will igonre the following things //hello lil //you entered:hello Switch import java . util . Scanner ; ​ public class switchDemo01 {     public static void main ( String [] args ) {         String name = "lilape" ;         Scanner scanner = new Scanner ( Syst...