import java.util.ArrayList;
class Demo02
{
public static void main(String[] args)
{
method3();
}
public static void method1(){
ArrayList<String> list = new ArrayList<String>();
list.add("后裔");
list.add("李元芳");
list.add("猴子");
System.out.println(list.get(0));
System.out.println(list.size());
for(int i = 0;i<list.size();i++){
System.out.println(list.get(i));
}
}
public static void method2(){
ArrayList<phone> arr = new ArrayList<phone>();
phone p1 = new phone();
p1.brands = "oppo";
p1.size = 5.5;
p1.yanse = "red";
arr.add(p1);
phone p2 = new phone();
p2.brands = "vivo";
p2.size = 4.7;
p2.yanse = "black";
arr.add(p2);
for(int i = 0;i<arr.size();i++){
System.out.println(arr.get(i).brands+" "+arr.get(i).size+" "+arr.get(i).yanse);
}
}
public static void method3(){
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(100);
arr.add(50);
//指定位置插入数值
arr.add(0,200);
//删除指定位置数值
arr.remove(1);
//清空
//arr.clear();
//修改指定位置元素
arr.set(0,30);
for(int i = 0;i<arr.size();i++){
System.out.println(arr.get(i));
}
}
}
public class Test01
{
public static void main(String[] args)
{
Dianfanguo dfg = new Dianfanguo();
Car c = new Car();
Student s = new Student();
dfg.brands = "格兰仕";
dfg.size = 20;
dfg.color = "红色";
System.out.println("品牌:"+dfg.brands);
System.out.println("尺寸:"+dfg.size);
System.out.println("颜色:"+dfg.color+"
");
c.brands = "科尼塞克";
c.size = "4.0L";
c.leixing = "跑车";
System.out.println("品牌:"+c.brands);
System.out.println("排量:"+c.size);
System.out.println("类型:"+c.leixing+"
");
s.name = "奥巴马";
s.age = 50;
s.sex = "男";
System.out.println("姓名:"+s.name);
System.out.println("年龄:"+s.age);
System.out.println("性别:"+s.sex+"
");
}
}
//ArrayList集合写随机点名
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
class zuoye
{
public static void main(String[] args)
{
//cunchu();
//bianli();
//suiji();
//cz();
//sj();
//yg();
}
public static void cunchu(){
ArrayList<Stu> arr = new ArrayList<Stu>();
Stu s0 = new Stu();
s0.name = "董斌";
s0.age = 20;
Stu s1 = new Stu();
s1.name = "张锐";
s1.age = 21;
Stu s2 = new Stu();
s2.name = "王荣臣";
s2.age = 22;
Stu s3 = new Stu();
s3.name = "韩凯";
s3.age = 23;
Stu s4 = new Stu();
s4.name = "张禄";
s4.age = 24;
Stu s5 = new Stu();
s5.name = "刘继勋";
s5.age = 25;
Stu s6 = new Stu();
s6.name = "张维";
s6.age = 26;
Stu s7 = new Stu();
s7.name = "牛攀";
s7.age = 28;
Stu s8 = new Stu();
s8.name = "魏中豪";
s8.age = 29;
arr.add(s0);
arr.add(s1);
arr.add(s2);
arr.add(s3);
arr.add(s4);
arr.add(s5);
arr.add(s6);
arr.add(s7);
arr.add(s8);
}
public static void bianli(ArrayList<Stu> arr){
ArrayList<Stu> arr = new ArrayList<Stu>();
Stu s0 = new Stu();
s0.name = "董斌";
s0.age = 20;
Stu s1 = new Stu();
s1.name = "张锐";
s1.age = 21;
Stu s2 = new Stu();
s2.name = "王荣臣";
s2.age = 22;
Stu s3 = new Stu();
s3.name = "韩凯";
s3.age = 23;
Stu s4 = new Stu();
s4.name = "张禄";
s4.age = 24;
Stu s5 = new Stu();
s5.name = "刘继勋";
s5.age = 25;
Stu s6 = new Stu();
s6.name = "张维";
s6.age = 26;
Stu s7 = new Stu();
s7.name = "牛攀";
s7.age = 28;
Stu s8 = new Stu();
s8.name = "魏中豪";
s8.age = 29;
arr.add(s0);
arr.add(s1);
arr.add(s2);
arr.add(s3);
arr.add(s4);
arr.add(s5);
arr.add(s6);
arr.add(s7);
arr.add(s8);
System.out.println("姓名 年龄");
for(int i = 0;i<arr.size();i++){
System.out.println(arr.get(i).name+" "+arr.get(i).age);
}
}
public static void suiji(){
Random r = new Random();
int num = r.nextInt(9);
ArrayList<Stu> arr = new ArrayList<Stu>();
Stu s0 = new Stu();
s0.name = "董斌";
s0.age = 20;
Stu s1 = new Stu();
s1.name = "张锐";
s1.age = 21;
Stu s2 = new Stu();
s2.name = "王荣臣";
s2.age = 22;
Stu s3 = new Stu();
s3.name = "韩凯";
s3.age = 23;
Stu s4 = new Stu();
s4.name = "张禄";
s4.age = 24;
Stu s5 = new Stu();
s5.name = "刘继勋";
s5.age = 25;
Stu s6 = new Stu();
s6.name = "张维";
s6.age = 26;
Stu s7 = new Stu();
s7.name = "牛攀";
s7.age = 28;
Stu s8 = new Stu();
s8.name = "魏中豪";
s8.age = 29;
arr.add(s0);
arr.add(s1);
arr.add(s2);
arr.add(s3);
arr.add(s4);
arr.add(s5);
arr.add(s6);
arr.add(s7);
arr.add(s8);
System.out.println(arr.get(num).name+" "+arr.get(num).age);
}
//ArrayList集合编写库存系统
public static void cz(){
while(true){
switch (xz())
{
case 1:show();
break;
case 2:xg();
break;
case 3:
return;
default:
System.out.println("输入有误");
break;
}
}
}
public static int xz(){
System.out.println("1、查看商品库存");
System.out.println("2、修改库存数量");
System.out.println("3、退出");
System.out.println("输入操作编号");
System.out.println("-----------------------------");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
return num;
}
public static void show(){
ArrayList<Product> arr = new ArrayList<Product>();
Product p0 = new Product();
p0.name = "MackBookAir";
p0.price = 6988.88;
p0.size = 13.3;
p0.count = 5;
arr.add(p0);
Product p1 = new Product();
p1.name = "ThinkpadT450";
p1.price = 5999.99;
p1.size = 14.0;
p1.count = 10;
arr.add(p1);
Product p2 = new Product();
p2.name = "ASUS-FL5800";
p2.price = 4999.5;
p2.size = 15.6;
p2.count = 18;
arr.add(p2);
System.out.println("----------------------商城库存清单--------------------");
System.out.println("品牌型号 尺寸 价格 库存数");
int csum = 0;
for(int i=0;i<arr.size();i++){
System.out.println(arr.get(i).name+" "+arr.get(i).size+" "+arr.get(i).price+" "+arr.get(i).count);
csum = csum + arr.get(i).count;
}
System.out.println("库存数 "+csum);
System.out.println("---------------123------------------");
}
public static void xg(){
ArrayList<Product> arr = new ArrayList<Product>();
Product p0 = new Product();
p0.name = "MackBookAir";
p0.price = 6988.88;
p0.size = 13.3;
p0.count = 5;
arr.add(p0);
Product p1 = new Product();
p1.name = "ThinkpadT450";
p1.price = 5999.99;
p1.size = 14.0;
p1.count = 10;
arr.add(p1);
Product p2 = new Product();
p2.name = "ASUS-FL5800";
p2.price = 4999.5;
p2.size = 15.6;
p2.count = 18;
arr.add(p2);
System.out.println("商品编号"+"品牌型号 尺寸 价格 库存数");
for(int i=0;i<arr.size();i++){
System.out.println(i+" "+arr.get(i).name+" "+arr.get(i).size+" "+arr.get(i).price+" "+arr.get(i).count);
}
System.out.println("----------------------------------------------------");
Scanner sc = new Scanner(System.in);
System.out.println("输入编号");
int num = sc.nextInt();
System.out.println("输入数量");
int n = sc.nextInt();
arr.get(num).count = n;
}
public static void sj(){
ArrayList<Pho> arr = new ArrayList<Pho>();
Pho p0 = new Pho();
p0.brand = "小米";
p0.price = 1999.0;
p0.color = "blue";
arr.add(p0);
Pho p1 = new Pho();
p1.brand = "华为";
p1.price = 3999.0;
p1.color = "white";
arr.add(p1);
Pho p2 = new Pho();
p2.brand = "乐视";
p2.price = 999.0;
p2.color = "black";
arr.add(p2);
Pho p3 = new Pho();
p3.brand = "奇酷";
p3.price = 2999;
p3.color = "red";
arr.add(p3);
for(int i = 0;i<arr.size();i++){
System.out.println(arr.get(i).brand+" "+arr.get(i).price+" "+arr.get(i).color);
}
}
public static void yg(){
ArrayList<Employee> arr = new ArrayList<Employee>();
Employee e0 = new Employee();
e0.name = "张三";
e0.num = 10;
e0.salary = 3000;
arr.add(e0);
Employee e1 = new Employee();
e1.name = "李四";
e1.num = 20;
e1.salary = 4000;
arr.add(e1);
Employee e2 = new Employee();
e2.name = "王五";
e2.num = 30;
e2.salary = 5000;
arr.add(e2);
for(int i = 0;i<arr.size();i++){
System.out.println(arr.get(i).name+" "+arr.get(i).num+" "+arr.get(i).salary);
}
}
//简述自定义数据类型的定义格式和使用方式?
//创建java文件,与类名相同
//public class 类名{
//数据类型 属性名称1;
// 数据类型 属性名称2;
// …
// }
//导包:我们将所有的类放到同一个文件夹下,可以避免导包。
// 创建对象:数据类型 变量名 = new 数据类型();
//调用方法:目前我们定义的自定义类不涉及方法,只是属性(自定义类中的方法部分在面向对象部分讲解)
// 访问属性:变量名.属性
//简述ArrayList集合的定义格式和常用方法的功能?
//导包:import java.util.ArrayList;
// 创建对象:与其他普通的引用数据类型创建方式完全相同,但是要指定容器中存储的数据类型:
// ArrayList<要存储元素的数据类型> 变量名 = new ArrayList<要存储元素的数据类型>();
//arr.add();
// arr.get();
// arr.size();
// arr.set();
// arr.remove();
// arr.clear();
}