• java列表数据基本操作


    列表数据组基本的增删改查

     1 package cn.edu.fhj.day002;
     2 
     3 import java.util.ArrayList;
     4 
     5 public class ArrList {
     6 //    定义一个主函数
     7     public static void main(String[] args) {
     8         // 创建一个用来装整数数据的arraylist对象
     9 //        ArrayList<Integer> xx = new ArrayList<Integer>();
    10         
    11         ArrayList<Integer> aa = new ArrayList<Integer>();
    12         
    13         // 向arraylist中添加数据
    14         aa.add(1);
    15         aa.add(2);
    16         aa.add(3);
    17         aa.add(4);
    18         // 从arraylist中读取数据
    19         System.out.println(aa.get(3));
    20         System.out.println(aa.get(2));
    21         System.out.println(aa.get(1));
    22         System.out.println(aa.get(0));
    23         System.out.println("循环取值");
    24         // 遍历list: 将整个arraylist的数据按脚标顺序全部打印出来
    25         for (int i = 0 ; i < aa.size();i++){
    26             System.out.println(aa.get(i));
    27         };
    28         // 从list中移除数据
    29         System.out.println("删除元素");
    30         aa.remove(0);
    31         for (int i = 0 ; i < aa.size();i++){
    32             System.out.println(aa.get(i));
    33         };
    34         // 更改list中指定位置上的数据
    35 //        int a = Integer.parseInt("8");
    36         System.out.println("更改元素");
    37         aa.set(1, 15);
    38         for (int i = 0; i < aa.size(); i++){
    39             System.out.println(aa.get(i));
    40         };
    41         
    42     }
    43     
    44     
    45     
    46 }

    列表数组demo

    package cn.edu.fhj.day002;
    
    import java.util.ArrayList;
    
    import day002.product.Product;
    
    
    
    public class ListDemo2 {
        
        public static void main(String[] args) {
            
            Product p1 = new Product();
            p1.pId = "1";
            p1.pName = "肥皂";
            p1.price = 2.5f;
            p1.number = 2;
            
            Product p2 = new Product();
            p2.pId = "2";
            p2.pName = "手铐";
            p2.price = 25.5f;
            p2.number = 2;
            
            Product p3 = new Product();
            p3.pId = "3";
            p3.pName = "鞭子";
            p3.price = 15.5f;
            p3.number = 1;
            
            Product p4 = new Product();
            p4.pId = "4";
            p4.pName = "锥子";
            p4.price = 18;
            p4.number = 2;
            
            
            Product p5 = new Product();
            p5.pId = "5";
            p5.pName = "风油精";
            p5.price = 8;
            p5.number = 2;
            
            ArrayList<Product> pdts  = new ArrayList<Product>();
            
            pdts.add(p1);
            pdts.add(p2);
            pdts.add(p3);
            pdts.add(p4);
            pdts.add(p5);
            
            // 计算list中的所有商品的总金额
            float amount = 0;
            for(int i=0;i<pdts.size();i++) {
                amount += (pdts.get(i).price * pdts.get(i).number);
            }
            
            System.out.println("总成交金额:" + amount);
            
            // 计算list中的所有商品中单价最高的商品
            
            Product tmp = pdts.get(0);
            for(int i=1;i<pdts.size();i++) {
                if(pdts.get(i).price > tmp.price) {
                    tmp = pdts.get(i);
                }
            }
            
            System.out.println("单价最高的商品为: " + tmp.toString());
            // 计算list中的所有商品中成交金额最高的商品
            tmp = pdts.get(0);
            for(int i=1;i<pdts.size();i++) {
                if(pdts.get(i).price*pdts.get(i).number > tmp.price*tmp.number) {
                    tmp = pdts.get(i);
                }
            }
            
            System.out.println("单成交金额最高的商品为: " +  tmp.toString());
            
            
            
            /**
             * 求出list中单价排名前3的商品
             */
            // 1、先按单价排序
            for(int i=0;i<pdts.size()-1;i++) {
                for(int j=0;j<pdts.size()-1-i;j++) {
                    if(pdts.get(j).price < pdts.get(j+1).price) {
                        Product t = pdts.get(j);
                        Product p_j1 = pdts.get(j+1);
                        pdts.set(j, p_j1); // 将脚标j位置上的数据换成j+1位置上的数据
                        pdts.set(j+1, t);  // 将脚标j+1位置上的数据换成j位置上的数据
                    }
                    
                }
            }
            
            // 2、再打印出前3个商品即为单价最高的3个商品
            System.out.println("单价最高前三名的商品为: ");
            for(int i=0;i<pdts.size();i++) {
                tmp = pdts.get(i);
                System.out.println(tmp.toString());
                System.out.println(tmp.pName);
                System.out.println(tmp.price);
            }
            
            
        }
    
    }

     代码模板

    1、ArrayList的用法 //------------------------------------------------------------------
    
    public class ListDemo{
        
        
        public static void main(String[] args){
            
            
            // 创建一个用来装整数数据的arraylist对象
            ArrayList<Integer> xx = new ArrayList<Integer>();
    
            // 向arraylist中添加数据
            xx.add(1);
            xx.add(3);
            xx.add(5);
            xx.add(7);
    
            // 从arraylist中读取数据
            int a = xx.get(1);
            System.out.println(a);
    
            System.out.println("------------------------");
            
            
            // 遍历list: 将整个arraylist的数据按脚标顺序全部打印出来
            for (int i = 0; i < xx.size(); i++) {
                System.out.println(xx.get(i));
            }
            
            
            System.out.println("------------------------");
            
            // 从list中移除数据
            xx.remove(1);
            for (int i = 0; i < xx.size(); i++) {
                System.out.println(xx.get(i));
            }
            
            // 更改list中指定位置上的数据
            xx.set(2, 15);
        
        }
    
    }
    
    
    
    // ------------ 方法的模板代码------------------------ // -----------------------------// 
    
    public class FunctionDemo{
        public int a;
        public int b;
        
        
        // 无参,无返回值   ----   方法示例
        
        public void sayHello(){
            System.out.println("我爱你,真的,很爱");
        }
        
        
        // 无参,有返回值   ----   方法示例
        public int getSelfSum(){
            
            return a+b;
            
        }
    
        // 有参,无返回值   ----   方法示例
        public void sayHelloToSomeOne(String name){
            
            System.out.println(name + "我爱你,真的,很爱");
            
        }
    
        
        // 有参,有返回值,且是静态  ----   方法示例
        public static  int getOtherSum(int x,int y){
            return x+y;
        }
        
        
        // 有参,有返回值,非静态  ----   方法示例
        public  int getOtherSum(int x){
    
            return this.a+this.b+x;
        }    
        
        
    }
    
    
    public class FunctionDemoTest{
        
        public static void main(String[] args){
            
            // 非静态方法,必须在对象上调
            
            FunctionDemo fd = new FunctionDemo();
            
            fd.sayHello();
            
            int selfSum = fd.getSelfSum();
            
            
            fd.sayHelloToSomeOne("芙蓉姐姐");
            
            // 静态方法可以在对象上调,但是没有必要
            // int otherSum = fd.getOtherSum(5,8);
            int otherSum = FunctionDemo.getOtherSum(5,8);
            
        }
        
        
        
    }
  • 相关阅读:
    Linux系统管理10-----进程和计划任务管理
    07作业进程和任务管理
    Linux系统管理09-----引导过程与服务过程
    网站部署流程
    Docker 安装与使用
    基于NFS共享实现kvm虚拟主机
    GlusterFS 部署
    rsync 远程同步服务部署
    Zabbix 通过 JMX 监控 tomcat
    Zabbix 简单错误 解决方法
  • 原文地址:https://www.cnblogs.com/cerofang/p/10232654.html
Copyright © 2020-2023  润新知