By GokiSoft.com|
20:02 20/05/2024|
Java Advanced
[Share Code] Tìm hiểu Exception & Collection trong Java - C2307L
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson01;
import java.util.ArrayList;
/**
*
* @author teacher
*/
public class Main {
static enum LIGHT_STATUS {ON, OFF}
public static void main(String[] args) {
/**
System.out.println("Hello World!!!");
Student std = new Student();
// System.out.println(5/0);
std.input();
std.display();
//Trang thai bat den / tat den -> den
int status;//status = 1 ->Sang, 0: Tat
LIGHT_STATUS st = LIGHT_STATUS.OFF;*/
int[] t1 = {1, 100};
int[] t2 = {3, 200, 1000};
System.arraycopy(t1, 0, t2, 0, t1.length);
for (int i = 0; i < t1.length; i++) {
t2[i] = t1[i];
}
System.out.println(t1.length);
System.out.println(t2.length);
for (int i : t2) {
System.out.println(i);
}
t1 = null;
t2 = null;
}
}
#readme.txt
Kiến thức tổng quát:
- Exception & Debug
- Collections
- List
- Set
- Map
- File
- Đọc/ghi nội dung dữ liệu (text, binary)
- Zip
- Thread
- CSDL
- Đa ngôn ngữ (Localization)
- Log & Gen Docs
- Lambda / Generic
UI:
Java Swing -> CSDL
Java FX -> Project (Scheme Builder)
==========================================================
Nội dung bài học hôm này:
- Exception & Debug
- Debug -> Tìm ra lỗi chương trình -> Dòng code nào -> Fix
- Collections
- List
- Set
- Map
#Student.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson01;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Student {
String fullname;
String address;
public Student() {
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
fullname = scan.nextLine();
System.out.println("Nhap dia chi: ");
address = scan.nextLine();
}
@Override
public String toString() {
return "Student{" + "fullname=" + fullname + ", address=" + address + '}';
}
public void display() {
System.out.println(this);
}
}
#Test.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson01;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
//Don gian -> cach nay
String s = "SINH VIEN APTECH";
s += "12";
s += "34";
s += "45";
s += "67";
System.out.println(s);
// s.concat("sdfsdf");
//Cach 2
StringBuilder builder = new StringBuilder();
builder.append("234234");
builder.append("234234");
builder.append("234234");
builder.append("234234");
builder.append("234234");
System.out.println(builder.toString());
//Cach 3
StringBuffer buffer = new StringBuffer();
buffer.append("234234");
buffer.append("234234");
buffer.append("234234");
buffer.append("234234");
buffer.append("234234");
System.out.println(buffer.toString());
}
}
#Test2.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson01;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author teacher
*/
public class Test2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap s1 = ");
String s1 = scan.nextLine();
System.out.println("Nhap s2 = ");
String s2 = scan.nextLine();
Pattern p = Pattern.compile(s2);
Matcher m = p.matcher(s1);
while(m.find()) {
System.out.println("finding > " + m.start());
}
//s1 -> Ton tai 1 String theo quy luat sau hay khong: [a-z]{2,3}[0-9]{3}[A-Z]{1-6}
String s3 = "[a-z]{2,3}[0-9]{3}[A-Z]{1,6}";
p = Pattern.compile(s3);
m = p.matcher(s1);
while(m.find()) {
System.out.println("###finding > " + m.start() + " > " + m.group());
}
}
}
#Test3.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson01;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;
/**
*
* @author teacher
*/
public class Test3 {
public static void main(String[] args) {
//ArrayList > Mang String
//Tao ra 1 mang rong elementData = {}
ArrayList<String> list = new ArrayList<>();
//Lan tao bo nho lan dau > mang rong 10 phan tu
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
for (String s : list) {
System.out.println(s);
}
//Tom tat: ArrayList -> tao 1 mang rong ->Them phan tu vao (Rong: 10 phan tu, lan sau -> tang theo cong thuc)
//ArrayList > Mang String
//Tao ra 1 mang gom 10 phan tu
Vector<String> v2 = new Vector<>();
//Lan tao bo nho lan dau > mang rong 10 phan tu
v2.add("1");
v2.add("2");
v2.add("3");
v2.add("4");
v2.add("5");
for (String s : v2) {
System.out.println(s);
}
//LinkedList > Mang String
LinkedList<String> v3 = new LinkedList<>();
//Lan tao bo nho lan dau > mang rong 10 phan tu
v3.add("1");
v3.add("2");
v3.add("3");
v3.add("4");
v3.add("5");
for (String s : v3) {
System.out.println(s);
}
//Map -> HashMap
HashMap<String, Student> maps = new HashMap<>();
Student std = new Student();
std.input();
maps.put("s1", std);
std = new Student();
std.input();
maps.put("s2", std);
std = maps.get("s1");
std.display();
for (Map.Entry<String, Student> entry : maps.entrySet()) {
String key = entry.getKey();
Student val = entry.getValue();
System.out.println(key);
val.display();
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)