ArrayList的使用
import java.util.*;
class testycf{
public static void main(String [] args){//ArrayList只能存放对象,所以在<>里面只能是基本数据类型的包装类。
ArrayList<String>c = new ArrayList<String>();//create List AND Insert
c.add("China");
c.add("GuiYang");
c.add("GZU");
//c.remove("GuiYang");//Delete
//c.get(1);//Index is 1 ,it's Data
c.set(1,"Shanghai");//update index=1 it's data
c.add(1,"A");//在A之后的元素下标都往后移动一位。
c.clear();//清空List列表。
//c.size();
System.out.println(c.size());
System.out.println(c);
for(String u:c)
System.out.print(u+" ");
}
}
ArrayList练习
import java.util.*;
class TestList{
public static void main(String [] args){
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> b1 =new ArrayList<Integer>();
int [][] array=new int[5][5];
int num=25;
for(int i=0;i<5;i++){
for(int j=0;j<(int)(Math.random()*5)+1;j++){
array[i][j]=num;
num--;
b1.add(array[i][j]);
System.out.print(array[i][j]+" ");
}
System.out.println();
}
a.add(b1);
System.out.println();
for(ArrayList<Integer> u:a){
for(Integer v:u){
System.out.print(v+" ");
}
}
}
}
import java.util.*;
class TestList{
public static void main(String [] args){
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> b1 =new ArrayList<Integer>();
int [][] array=new int[5][5];
int num=25;
for(int i=0;i<5;i++){
for(int j=0;j<(int)(Math.random()*5)+1;j++){
array[i][j]=num;
num--;
b1.add(array[i][j]);
System.out.print(array[i][j]+" ");
}
System.out.println();
}
System.out.println();
for(int i=0;i<5;i++){
Arrays.sort(array[i]);
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(array[i][j]!=0)
System.out.print(array[i][j]+" ");
}
System.out.println();
}
a.add(b1);
System.out.println();
for(ArrayList<Integer> u:a){
for(Integer v:u){
System.out.print(v+" ");
}
}
}
}
3
import java.util.*;
class TestArrayList{
public static void main(String [] args){
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i =0;i<10;i++){
a.add((int)(Math.random()*100));
}
Collections.sort(a);
for(Integer u:a){
System.out.print(a+" ");
}
}
}
修改
import java.util.*;
class TestArrayList{
public static void main(String [] args){
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i =0;i<100;i++){
a.add((int)(Math.random()*100));
}
Collections.sort(a);
//for(Integer u:a)
System.out.println(a+" ");
}
}
4
import java.util.Scanner;
import java.util.ArrayList;
class Test425 {
public static void main(String[] args)
{
System.out.println("Enter 5 integers for list1: ");
Scanner input = new Scanner(System.in);
ArrayList<Integer> L1 = new ArrayList<>();
for(int i=0;i<5;i++){
int number0 = input.nextInt();
L1.add(number0);
}
System.out.println("Enter 5 integers for list2: ");
ArrayList<Integer> L2 = new ArrayList<>();
for(int i=0;i<5;i++){
int number = input.nextInt();
L2.add(number);
}
ArrayList<Integer> dio = union(L1,L2);
for(int j:dio)
System.out.print(j+" ");
}
public static ArrayList<Integer> union(
ArrayList<Integer> list1,ArrayList<Integer> list2
){
ArrayList<Integer> temp = new ArrayList<>();
temp.addAll(list1);
temp.addAll(list2);
return temp;
}
}