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

  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. }

列表数组demo

  1. package cn.edu.fhj.day002;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import day002.product.Product;
  6.  
  7. public class ListDemo2 {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. Product p1 = new Product();
  12. p1.pId = "1";
  13. p1.pName = "肥皂";
  14. p1.price = 2.5f;
  15. p1.number = 2;
  16.  
  17. Product p2 = new Product();
  18. p2.pId = "2";
  19. p2.pName = "手铐";
  20. p2.price = 25.5f;
  21. p2.number = 2;
  22.  
  23. Product p3 = new Product();
  24. p3.pId = "3";
  25. p3.pName = "鞭子";
  26. p3.price = 15.5f;
  27. p3.number = 1;
  28.  
  29. Product p4 = new Product();
  30. p4.pId = "4";
  31. p4.pName = "锥子";
  32. p4.price = 18;
  33. p4.number = 2;
  34.  
  35. Product p5 = new Product();
  36. p5.pId = "5";
  37. p5.pName = "风油精";
  38. p5.price = 8;
  39. p5.number = 2;
  40.  
  41. ArrayList<Product> pdts = new ArrayList<Product>();
  42.  
  43. pdts.add(p1);
  44. pdts.add(p2);
  45. pdts.add(p3);
  46. pdts.add(p4);
  47. pdts.add(p5);
  48.  
  49. // 计算list中的所有商品的总金额
  50. float amount = 0;
  51. for(int i=0;i<pdts.size();i++) {
  52. amount += (pdts.get(i).price * pdts.get(i).number);
  53. }
  54.  
  55. System.out.println("总成交金额:" + amount);
  56.  
  57. // 计算list中的所有商品中单价最高的商品
  58.  
  59. Product tmp = pdts.get(0);
  60. for(int i=1;i<pdts.size();i++) {
  61. if(pdts.get(i).price > tmp.price) {
  62. tmp = pdts.get(i);
  63. }
  64. }
  65.  
  66. System.out.println("单价最高的商品为: " + tmp.toString());
  67. // 计算list中的所有商品中成交金额最高的商品
  68. tmp = pdts.get(0);
  69. for(int i=1;i<pdts.size();i++) {
  70. if(pdts.get(i).price*pdts.get(i).number > tmp.price*tmp.number) {
  71. tmp = pdts.get(i);
  72. }
  73. }
  74.  
  75. System.out.println("单成交金额最高的商品为: " + tmp.toString());
  76.  
  77. /**
  78. * 求出list中单价排名前3的商品
  79. */
  80. // 1、先按单价排序
  81. for(int i=0;i<pdts.size()-1;i++) {
  82. for(int j=0;j<pdts.size()-1-i;j++) {
  83. if(pdts.get(j).price < pdts.get(j+1).price) {
  84. Product t = pdts.get(j);
  85. Product p_j1 = pdts.get(j+1);
  86. pdts.set(j, p_j1); // 将脚标j位置上的数据换成j+1位置上的数据
  87. pdts.set(j+1, t); // 将脚标j+1位置上的数据换成j位置上的数据
  88. }
  89.  
  90. }
  91. }
  92.  
  93. // 2、再打印出前3个商品即为单价最高的3个商品
  94. System.out.println("单价最高前三名的商品为: ");
  95. for(int i=0;i<pdts.size();i++) {
  96. tmp = pdts.get(i);
  97. System.out.println(tmp.toString());
  98. System.out.println(tmp.pName);
  99. System.out.println(tmp.price);
  100. }
  101.  
  102. }
  103.  
  104. }

代码模板

  1. 1ArrayList的用法 //------------------------------------------------------------------
  2.  
  3. public class ListDemo{
  4.  
  5. public static void main(String[] args){
  6.  
  7. // 创建一个用来装整数数据的arraylist对象
  8. ArrayList<Integer> xx = new ArrayList<Integer>();
  9.  
  10. // 向arraylist中添加数据
  11. xx.add(1);
  12. xx.add(3);
  13. xx.add(5);
  14. xx.add(7);
  15.  
  16. // 从arraylist中读取数据
  17. int a = xx.get(1);
  18. System.out.println(a);
  19.  
  20. System.out.println("------------------------");
  21.  
  22. // 遍历list: 将整个arraylist的数据按脚标顺序全部打印出来
  23. for (int i = 0; i < xx.size(); i++) {
  24. System.out.println(xx.get(i));
  25. }
  26.  
  27. System.out.println("------------------------");
  28.  
  29. // 从list中移除数据
  30. xx.remove(1);
  31. for (int i = 0; i < xx.size(); i++) {
  32. System.out.println(xx.get(i));
  33. }
  34.  
  35. // 更改list中指定位置上的数据
  36. xx.set(2, 15);
  37.  
  38. }
  39.  
  40. }
  41.  
  42. // ------------ 方法的模板代码------------------------ // -----------------------------//
  43.  
  44. public class FunctionDemo{
  45. public int a;
  46. public int b;
  47.  
  48. // 无参,无返回值 ---- 方法示例
  49.  
  50. public void sayHello(){
  51. System.out.println("我爱你,真的,很爱");
  52. }
  53.  
  54. // 无参,有返回值 ---- 方法示例
  55. public int getSelfSum(){
  56.  
  57. return a+b;
  58.  
  59. }
  60.  
  61. // 有参,无返回值 ---- 方法示例
  62. public void sayHelloToSomeOne(String name){
  63.  
  64. System.out.println(name + "我爱你,真的,很爱");
  65.  
  66. }
  67.  
  68. // 有参,有返回值,且是静态 ---- 方法示例
  69. public static int getOtherSum(int x,int y){
  70. return x+y;
  71. }
  72.  
  73. // 有参,有返回值,非静态 ---- 方法示例
  74. public int getOtherSum(int x){
  75.  
  76. return this.a+this.b+x;
  77. }
  78.  
  79. }
  80.  
  81. public class FunctionDemoTest{
  82.  
  83. public static void main(String[] args){
  84.  
  85. // 非静态方法,必须在对象上调
  86.  
  87. FunctionDemo fd = new FunctionDemo();
  88.  
  89. fd.sayHello();
  90.  
  91. int selfSum = fd.getSelfSum();
  92.  
  93. fd.sayHelloToSomeOne("芙蓉姐姐");
  94.  
  95. // 静态方法可以在对象上调,但是没有必要
  96. // int otherSum = fd.getOtherSum(5,8);
  97. int otherSum = FunctionDemo.getOtherSum(5,8);
  98.  
  99. }
  100.  
  101. }

java列表数据基本操作的更多相关文章

  1. Java学习-054-Mybatis IN查询,通过 foreach 获取列表数据

    通过如下语句查询商品订单信息: ,,,) 在 Mapper.java 中定义如下接口: List<GoodsOrder> findGoodsOrderByIds(String ids); ...

  2. 开源 免费 java CMS - FreeCMS1.9 移动APP生成网站列表数据

    项目地址:http://www.freeteam.cn/ 生成网站列表数据 提取同意移动APP訪问的网站列表,生成json数据到/mobile/index.html页面. 从左側管理菜单点击生成网站列 ...

  3. 开源 免费 java CMS - FreeCMS1.9 移动APP生成栏目列表数据

    项目地址:http://www.freeteam.cn/ 生成栏目列表数据 提取当前管理网站下同意移动APP訪问的栏目列表,生成json数据到/site/网站文件夹/mobile/channels.h ...

  4. java之文件基本操作

    java之文件基本操作 1 使用 BufferedReader 在控制台读取字符 public static void readChar() throws IOException{ char c; I ...

  5. Java列表

    Java列表踩过的坑 其中subList是RandomAccessSubList,不是序列化的列表,不可以加入tair. 加入tair测试代码 @Autowired private CacheMana ...

  6. js数组(列表)的基本操作

    本文主要介绍JS对数组(列表)的基本操作.习惯了用数据库的操作顺序来说明:增.删.改.查:合并,裁剪,排序,格式化. 一.数组元素的添加(增加) 增加数组元素有三种方法:unshift()  push ...

  7. 跟我一起学extjs5(37--单个模块的设计[5取得模块列表数据])

    跟我一起学extjs5(37--单个模块的设计[5取得模块列表数据])         写了几个月,总算有点盼头了,最终要从后台取得数据了.后台的spring mvc 和 service 仅仅能简单的 ...

  8. MyBatis基础:MyBatis数据基本操作(2)

    1. MyBatis映射器 2. MyBatis数据基本操作 示例项目结构: <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...

  9. [Python 从入门到放弃] 1. 列表的基本操作

    ''' 列表 Create By 阅后即焚 On 2018.1.29 ''' 1. 列表的定义 列表看起来好像其它编程语言中的数组,但列表具备更加强大的功能,它是Python完备的集合对象,现在,你可 ...

随机推荐

  1. Battery Historian 使用常用命令

    一.重置电池数据收集数据 打开电池数据获取:adb shell dumpsys batterystats --enable full-wake-history 重置电池数据: adb shell du ...

  2. Sobel 边缘检测算子

    转自:http://blog.csdn.net/xiaqunfeng123/article/details/17302003 Sobel 算子是一个离散微分算子 (discrete different ...

  3. 6核 CPU导致SQL2005安装时出“无法启动服务”错

    周一新买的IBM3650M3的服务器上安装SQL server2005 安装到一半时,报"提示:SQL Server 服务无法启动."错. 换了几个操作系统版本和换了几个版本的sq ...

  4. 关于使用python ~取反操作符带出的一系列问题

    晚上的时候,无意之间看到stackoverflow上面的一个编程挑战赛,各路高手各种搞事,看到python的地方突然发现用了很多位运算的符号,但是~符号引起了我和同事的注意. 我们很少在程序中使用这种 ...

  5. [再寄小读者之数学篇](2014-06-23 Hardy 空间、BMO空间与 Triebel-Lizorkin 空间)

    $$\bex 0<p<\infty\ra H_p=\dot F^0_{p,2};\quad BMO=\dot F^0_{\infty,2}. \eex$$ see [H. Triebel, ...

  6. 弄懂promise

    ECMAscript 6 原生提供了 Promise 对象. Promise 对象代表了未来将要发生的事件,用来传递异步操作的消息 有了 Promise 对象,就可以将异步操作以同步操作的流程表达出来 ...

  7. 论文阅读笔记:《Contextual String Embeddings for Sequence Labeling》

    文章引起我关注的主要原因是在CoNLL03 NER的F1值超过BERT达到了93.09左右,名副其实的state-of-art.考虑到BERT训练的数据量和参数量都极大,而该文方法只用一个GPU训了一 ...

  8. CF1119B Alyona and a Narrow Fridge

    题目地址:CF1119B Alyona and a Narrow Fridge \(O(n^2)\) 暴力枚举+贪心 从小到大枚举答案 假设枚举到 \(i\) ,将 \(a_1\) 到 \(a_i\) ...

  9. 【转】Python3 操作符重载方法

    Python3 操作符重载方法 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/70769628 以下代码 ...

  10. Lua中的环境概念

    [前言] Lua将其所有的全局变量保存在一个常规的table中,这个table称为“环境”.这种组织结构的优点在于,其一,不需要再为全局变量创造一种新的数据结构,因此简化了Lua的内部实现:另一个优点 ...