By GokiSoft.com|
20:34 12/04/2024|
Java Basic
[Share Code] Tìm hiểu Loop & Array trong Java - C2307L
Nội dung học:
- Loop (For, while, do ... while, foreach)
- Array
- Array Index
- ArrayList
- Vector
==========================================================
#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.c2307l.lesson02;
import java.util.ArrayList;
/**
*
* @author diepvan
*/
public class Test {
public static void main(String[] args) {
//B1. Khai bao mang dong -> Su dung ArrayList
//length: 0, index: 0 -> length - 1
//lenght: iList.size()
ArrayList<Integer> iList = new ArrayList<>();
//B2. Them phan tu vao trong mang
iList.add(100);//length: 1, index: 0 -> {100}
iList.add(20);//length: 2, index: 0 - 1 -> {100, 20}
iList.add(11);//length: 3, index: 0 - 2 -> {100, 20, 11}
//B3. Lay phan tu trong mang ra
//index = 1
System.out.println("index:1 -> " + iList.get(1));
for (int i = 0; i < iList.size(); i++) {
System.out.println("index:" + i +" -> " + iList.get(i));
}
//foreach
for (Integer v : iList) {
System.out.println("v = " + v);
}
//B4. Xoa phan tu trong mang
//Xoa phan tu index = 1
iList.remove(1);//length: 2, index: 0 - 1 -> {100, 11}
//B5. Chen phan tu vao trong mang
//Chen phan tu 200 vao vi tri index = 1
iList.add(1, 200); //length: 3, index: 0 - 2 -> {100, 200, 11}
//B6. Thay du lieu trong mang
//Thay gia tri tai vi tri index: 1 -> Thanh 3000
iList.set(1, 3000);//length: 3, index: 0 - 2 -> {100, 3000, 11}
//B7. Xoa tat ca cac phan tu trong mang
iList.clear(); //length: 0, index: -> {}
//Tuong tu -> tao mang dong: String, char, double, float, ...
ArrayList<String> sList = new ArrayList<>();
for (String s : sList) {
System.out.println("s = " + s);
}
ArrayList<Character> cList = new ArrayList<>();
for (Character c : cList) {
System.out.println("c = " + c);
}
}
}
#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.c2307l.lesson02;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
public static void main(String[] args) {
int num1 = 1;
int num2 = 30;
/**
* num1 = 14, num2 = 17 -> num1 = 15, num2 = 16
* num1 = 15, num2 = 16 -> num1 = 16, num2 = 15
* num1 = 16, num2 = 15 -> num1 = 17, num2 = 14
*/
while(num1++ < num2--);
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
Scanner abc = new Scanner(System.in);
//Phan 2: Array Index -> Vi du la Array index: Int
//B1. Khai bao mang
//Do dai cua mang -> 7 (t.length, m.length)
//Giai index: 0 -> length - 1 (0 -> 6)
int[] t = new int[7];
int m[] = new int[7];
//B2. Them phan tu vao trong
//Xac dinh vi tri can them du lieu vao
//index: 1 -> 20
t[1] = 20;
//Bo sung du lieu theo 3, 8, 1, 0, 5, -2, 32
t[0] = 3;
t[1] = 8;
t[2] = 1;
t[3] = 0;
t[4] = 5;
t[5] = -2;
t[6] = 32;
for (int i = 0; i < 7; i++) {
System.out.println("Nhap t["+i+"] = ");
t[i] = Integer.parseInt(abc.nextLine());//Nhap string: abc.nextLine() -> Chuyen string thanh int: Integer.parseInt
//abc.nextInt()
}
//B3. Lay phan tu trong mang ra
//Lay du lieu tai vi tri nao -> index: 2
System.out.println("t[2] = " + t[2]);
for (int i = 0; i < t.length; i++) {
System.out.println("t["+i+"]=" + t[i]);
}
//B4. Xoa phan tu trong mang -> Voi mang index -> reset du lieu = 0
//index: 2
t[2] = 0;
//B5. Xoa tao bo du lieu
t = new int[7];
//Khai bao mang String, char, boolean, float, double, ...
String[] list = new String[20];
char[] cList = new char[30];
//Van de gap phai
//t -> gom 7 phan tu. Bai toan -> Mo rong mang len 20 phan tu
//Chung ta se lam the nao???
t = new int[20];//Gap van de gi -> mat du lieu da nhap truoc do.
//Lam sao de ko mat du lieu da nhap truoc do???
int k[] = new int[20];
//Copy du lieu tu t -> k
for (int i = 0; i < t.length; i++) {
k[i] = t[i];
}
//Nhap du lieu tiep theo cho k
//Gan lai k -> t
t = new int[20];
//copy k -> t
//Lam nhu nay -> kha mat tgian code
//Dang can 1 mang dong
//So phan tu trong mang -> thay doi duoc: 7, 20, 2, 11, 100, ...
//Chung ta se lam cach nao???
//Array Index: 30, 11, 20, 100, 44
int kk[] = {30, 11, 20, 100, 44};//length: 5, index: 0 -> 4
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)