• Thế Giới Giải Mã

    Bí ẩn nhân loại Leonardo Da Vinci

  • Thế Giới Giải Mã

    Anh hùng thầm lặng Nikola Tesla

  • Thế Giới Giải Mã

    Thần đèn Thomas Edison

  • Thế Giới Giải Mã

    Người thôi miên Adolf Hitler

Showing posts with label Java Core. Show all posts
Showing posts with label Java Core. Show all posts

01 November 2018

Loại bỏ giá trị trùng nhau duplicate trong ArrayList bằng HashSet lập trình Java

Loại bỏ giá trị trùng nhau trong ArrayList 
Loại bỏ giá trị trùng nhau trong ArrayList 
Java Core 2018
package test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Demo {

   public static void main(String args[]) {

      List < String > list = new ArrayList < String > ();
      list.add("Brishna");
      list.add("Eishan");
      list.add("Aryan");
      list.add("Darm");
      list.add("Darm");
      list.add("Darm");

      System.out.println(list);

      Set < String > set = new HashSet < > (list);

      System.out.println(set);

   }
}

Output:
[Brishna, Eishan, Aryan, Darm, Darm, Darm]
[Brishna, Darm, Aryan, Eishan]
Kiến Thức Cần Có:
1. List, ArrayList, Set, HashSet là gì ?
2. println là gì ?

Hàm đảo ngược Reverse ArrayList bằng thuật toán dùng vòng lặp For trong Java

Reverse ArrayList bằng thuật toán dùng vòng lặp For
Hàm đảo ngược Reverse ArrayList bằng For Loop
Java Core 2018
package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo {

   public int temporary;

   public Demo() {
      super();
   }

   public static void main(String args[]) {

      Demo d = new Demo();

      List < Integer > list = new ArrayList < Integer > ();
      list.add(1);
      list.add(3);
      list.add(5);
      list.add(6);

      // Integer list origin
      System.out.println(list);

      // Convert ArrayList to array
      Integer[] array = (Integer[]) list.toArray(new Integer[list.size()]);

      // Reverse function
      for (int i = 0; i < array.length / 2; i++) {
         d.temporary = array[i]; //temporary là biến tạm lưu giá trị
         array[i] = array[array.length - 1 - i]; //lấy giá trị cuối lưu vào đầu
         array[array.length - 1 - i] = d.temporary; //lấy giá trị đầu lưu vào cuối
      }
      // Convert array to List
      System.out.println(Arrays.asList(array));
   }
}

Output:
[1, 3, 5, 6]
[6, 5, 3, 1]
Kiến Thức Cần Có:
1. Khai báo kiểu int ?
2. List, ArrayList và Arrays là như thế nào ?
3. println là gì ?
5. Vòng lặp for là gì ?
6. toArray là gì ?
7. asList là gì ?

Hàm đảo ngược mảng trong lập trình java sử dụng vòng lặp for

Hàm Reverse mảng trong lập trình java
Hàm đảo ngược mảng bằng vòng lặp for 
Java Core 2018
package test;

import java.util.Arrays;

public class Demo {

   public int temporary;

   public Demo() {}

   public void reverse(Demo d) {
      // Integer array origin
      Integer[] array = {1,3,5,7};

      // Convert array to List
      System.out.println(Arrays.asList(array));

      // Reverse logic
      for (int i = 0; i < array.length / 2; i++) {
         d.temporary = array[i]; //temporary là biến tạm lưu giá trị
         array[i] = array[array.length - 1 - i]; //lấy giá trị cuối lưu vào đầu
         array[array.length - 1 - i] = d.temporary; //lấy giá trị đầu lưu vào cuối
      }
      // Convert array to List
      System.out.println(Arrays.asList(array));
   }

   public static void main(String args[]) {
      Demo d = new Demo();
      d.reverse(d);
   }
}

Output:
[1, 3, 5, 7]
[7, 5, 3, 1]
Kiến Thức Cần Có:
1. Khai báo kiểu int, Integer là gì, Cách khai bảo mảng ?
3. println là gì ?
5. Vòng lặp for là gì ?
6. toArray là gì ?
7. asList là gì ?

Hàm đảo ngược Reverse LinkedList bằng thuật toán dùng vòng lặp For trong Java

Reverse LinkedList bằng thuật toán dùng vòng lặp For
Hàm đảo ngược Reverse LinkedList bằng For Loop
Java Core 2018
package test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Demo {

   public int temporary;

   public Demo() {
      super();
   }

   public static void main(String args[]) {

      Demo d = new Demo();

      List < Integer > list = new LinkedList < Integer > ();
      list.add(1);
      list.add(3);
      list.add(5);
      list.add(6);

      // Integer list origin
      System.out.println(list);

      // Convert LinkedList to array
      Integer[] array = (Integer[]) list.toArray(new Integer[list.size()]);

      // Reverse function
      for (int i = 0; i < array.length / 2; i++) {
         d.temporary = array[i]; //temporary là biến tạm lưu giá trị
         array[i] = array[array.length - 1 - i]; //lấy giá trị cuối lưu vào đầu
         array[array.length - 1 - i] = d.temporary; //lấy giá trị đầu lưu vào cuối
      }
      // Convert array to List
      System.out.println(Arrays.asList(array));
   }
}

Output:
[1, 3, 5, 6]
[6, 5, 3, 1]
Kiến Thức Cần Có:
1. Khai báo kiểu int , Integer là gì, khai báo kiểu mảng ?
2. List, LinkedList và Arrays là như thế nào ?
3. println là gì ?
5. Vòng lặp for là gì ?
6. toArray là gì ?
7. asList là gì ?

Hàm sắp xếp LinkedList tăng dần bằng "Thuật toán nổi bọt" trong lập trình Java

Sắp xếp LinkedList tăng dần với Thuật toán nổi bọt
Hàm sắp xếp LinkedList tăng dần bằng mảng trong lập trình Java
Thuật toán nổi bọt
Hàm sắp xếp LinkedList tăng dần bằng mảng
Java Core 2016
package test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Demo {
   public String strTemp;
   public Integer numTemp;

   public Demo() {
      super();
   }

   public static void main(String args[]) {

      Demo d = new Demo();

      List < String > strList = new LinkedList < String > ();
      strList.add("D");
      strList.add("A");
      strList.add("C");
      strList.add("B");

      // String list origin
      System.out.println(strList);

      // Convert LinkedList to array
      String[] strArray = (String[]) strList.toArray(new String[strList.size()]);

      for (int i = 0; i < strArray.length - 1; i++) {
         for (int j = i + 1; j < strArray.length; j++) {
            // sap xep tang dan > 0 hoac giam dan < 0
            if (strArray[i].compareTo(strArray[j]) > 0) {
               d.strTemp = strArray[i];
               strArray[i] = strArray[j];
               strArray[j] = d.strTemp;
            }
         }
      }
      // Convert strArray to List
      System.out.println(Arrays.asList(strArray));

      List < Integer > numList = new LinkedList < Integer > ();
      numList.add(6);
      numList.add(5);
      numList.add(3);
      numList.add(1);
      numList.add(8);
      numList.add(7);
      numList.add(2);
      numList.add(4);

      // Integer list origin
      System.out.println(numList);

      // Convert LinkedList to array
      Integer[] numArray = (Integer[]) numList.toArray(new Integer[numList.size()]);

      for (int i = 0; i < numArray.length - 1; i++) {
         for (int j = i + 1; j < numArray.length; j++) {
            // sap xep tang dan a[i] > a[j] or giam dan a[i] < a[j]
            if (numArray[i] > numArray[j]) {
               d.numTemp = numArray[i]; //numTemp là biến tạm lưu giá trị
               numArray[i] = numArray[j]; //thực hiện đổi chỗ 'dồi chó'
               numArray[j] = d.numTemp;
            }
         }
      }
      // Convert strArray to List
      System.out.println(Arrays.asList(numArray));
   }
}
Output:
[D, A, C, B]
[A, B, C, D]
[6, 5, 3, 1, 8, 7, 2, 4]
[1, 2, 3, 4, 5, 6, 7, 8]
Kiến Thức Cần Có:
1. Khai báo kiểu String và Integer, khai báo mảng ?
2. List, LinkedList và Arrays là như thế nào ?
3. println là gì ?
5. Vòng lặp for là gì ?
6. toArray là gì ?
7. asList là gì ?

11 June 2017

Java Core: Thêm và Xóa phần tử mảng Array trên JAVA


Ouput:
SimpleStack.java
Java Core 2017
package com.array; /** * Created by Antonio 11/06/17. */ public class SimpleStack { char charr[] = new char[5]; int top = -1; public boolean isEmpty() { if (charr[0] == '\0') { return true; } return false; } public boolean isFull() { if (charr.length == top) { return true; } else { return false; } } public void push(char ch) { if (!isFull()) { top++; charr[top] = ch; } else { System.out.println("stack is full"); } } public char pop() { char chh = charr[top]; if (!isEmpty()) { charr[top] = '\0'; top--; } else { System.out.println("stack is empty"); } return chh; } public void display() { System.out.println(); for (int i = 0; i < charr.length; i++) { System.out.printf("\t -->" + charr[i]); } } public static void main(String[] args) { SimpleStack simpleStack = new SimpleStack(); // simpleStack.pop(); simpleStack.push('a'); simpleStack.push('b'); simpleStack.push('c'); simpleStack.push('d'); simpleStack.push('e'); simpleStack.display(); simpleStack.pop(); simpleStack.push('F'); simpleStack.display(); } }

15 April 2017

Java: Đa luồng trong Java Multithreading in Java

1)New

The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

2)Runnable

The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

3)Running

The thread is in running state if the thread scheduler has selected it.

4)Non-Runnable (Blocked)

This is the state when the thread is still alive, but is currently not eligible to run.

5)Terminated

A thread is in terminated or dead state when its run() method exits.

Làm thế nào để tạo Thread:

Chúng ta có hai cách tạo Thread:
  1. Bằng extending Thread class
  2. Bằng implementing Runnable interface.

Thread class:

Lớp Thread cung cấp Constructors và Methods để tạo và thực hiện các thao tác trên một luồng.
Sử dụng Constructors của Thread:

  • Thread()
  • Thread(String name)
  • Thread(Runnable r)
  • Thread(Runnable r,String name)

Sử dụng Methods của Thread:

  1. public void run(): is used to perform action for a thread.
  2. public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
  3. public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
  4. public void join(): waits for a thread to die.
  5. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
  6. public int getPriority(): returns the priority of the thread.
  7. public int setPriority(int priority): changes the priority of the thread.
  8. public String getName(): returns the name of the thread.
  9. public void setName(String name): changes the name of the thread.
  10. public Thread currentThread(): returns the reference of currently executing thread.
  11. public int getId(): returns the id of the thread.
  12. public Thread.State getState(): returns the state of the thread.
  13. public boolean isAlive(): tests if the thread is alive.
  14. public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
  15. public void suspend(): is used to suspend the thread(depricated).
  16. public void resume(): is used to resume the suspended thread(depricated).
  17. public void stop(): is used to stop the thread(depricated).
  18. public boolean isDaemon(): tests if the thread is a daemon thread.
  19. public void setDaemon(boolean b): marks the thread as daemon or user thread.
  20. public void interrupt(): interrupts the thread.
  21. public boolean isInterrupted(): tests if the thread has been interrupted.
  22. public static boolean interrupted(): tests if the current thread has been interrupted.

Runnable interface:

Giao diện Runnable chỉ có một phương thức có tên run ().
  1. public void run(): Được sử dụng để thực hiện hành động cho Thread.

1) Bằng extending Thread class:

  1. class Multi extends Thread{  
  2.      public void run(){  
  3.          System.out.println("thread is running...");  
  4.     }  
  5.      public static void main(String args[]){  
  6.          Multi t1=new Multi();  
  7.          t1.start();  
  8.     }  
  9. }  
Output:thread is running...

2) Bằng implementing Runnable interface:

  1. class Multi3 implements Runnable{  
  2.       public void run(){  
  3.           System.out.println("thread is running...");  
  4.      }  
  5.       public static void main(String args[]){  
  6.           Multi3 m1=new Multi3();  
  7.           Thread t1 =new Thread(m1);  
  8.           t1.start();  
  9.      }  
  10. }  
Output:thread is running...

Tìm hiểu thêm tại đây

 

BACK TO TOP

Xuống cuối trang