java列表数据基本操作
列表数据组基本的增删改查
- package cn.edu.fhj.day002;
- import java.util.ArrayList;
- public class ArrList {
- // 定义一个主函数
- public static void main(String[] args) {
- // 创建一个用来装整数数据的arraylist对象
- // ArrayList<Integer> xx = new ArrayList<Integer>();
- ArrayList<Integer> aa = new ArrayList<Integer>();
- // 向arraylist中添加数据
- aa.add(1);
- aa.add(2);
- aa.add(3);
- aa.add(4);
- // 从arraylist中读取数据
- System.out.println(aa.get(3));
- System.out.println(aa.get(2));
- System.out.println(aa.get(1));
- System.out.println(aa.get(0));
- System.out.println("循环取值");
- // 遍历list: 将整个arraylist的数据按脚标顺序全部打印出来
- for (int i = 0 ; i < aa.size();i++){
- System.out.println(aa.get(i));
- };
- // 从list中移除数据
- System.out.println("删除元素");
- aa.remove(0);
- for (int i = 0 ; i < aa.size();i++){
- System.out.println(aa.get(i));
- };
- // 更改list中指定位置上的数据
- // int a = Integer.parseInt("8");
- System.out.println("更改元素");
- aa.set(1, 15);
- for (int i = 0; i < aa.size(); i++){
- System.out.println(aa.get(i));
- };
- }
- }
列表数组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);
- }
- }
java列表数据基本操作的更多相关文章
- Java学习-054-Mybatis IN查询,通过 foreach 获取列表数据
通过如下语句查询商品订单信息: ,,,) 在 Mapper.java 中定义如下接口: List<GoodsOrder> findGoodsOrderByIds(String ids); ...
- 开源 免费 java CMS - FreeCMS1.9 移动APP生成网站列表数据
项目地址:http://www.freeteam.cn/ 生成网站列表数据 提取同意移动APP訪问的网站列表,生成json数据到/mobile/index.html页面. 从左側管理菜单点击生成网站列 ...
- 开源 免费 java CMS - FreeCMS1.9 移动APP生成栏目列表数据
项目地址:http://www.freeteam.cn/ 生成栏目列表数据 提取当前管理网站下同意移动APP訪问的栏目列表,生成json数据到/site/网站文件夹/mobile/channels.h ...
- java之文件基本操作
java之文件基本操作 1 使用 BufferedReader 在控制台读取字符 public static void readChar() throws IOException{ char c; I ...
- Java列表
Java列表踩过的坑 其中subList是RandomAccessSubList,不是序列化的列表,不可以加入tair. 加入tair测试代码 @Autowired private CacheMana ...
- js数组(列表)的基本操作
本文主要介绍JS对数组(列表)的基本操作.习惯了用数据库的操作顺序来说明:增.删.改.查:合并,裁剪,排序,格式化. 一.数组元素的添加(增加) 增加数组元素有三种方法:unshift() push ...
- 跟我一起学extjs5(37--单个模块的设计[5取得模块列表数据])
跟我一起学extjs5(37--单个模块的设计[5取得模块列表数据]) 写了几个月,总算有点盼头了,最终要从后台取得数据了.后台的spring mvc 和 service 仅仅能简单的 ...
- MyBatis基础:MyBatis数据基本操作(2)
1. MyBatis映射器 2. MyBatis数据基本操作 示例项目结构: <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...
- [Python 从入门到放弃] 1. 列表的基本操作
''' 列表 Create By 阅后即焚 On 2018.1.29 ''' 1. 列表的定义 列表看起来好像其它编程语言中的数组,但列表具备更加强大的功能,它是Python完备的集合对象,现在,你可 ...
随机推荐
- Battery Historian 使用常用命令
一.重置电池数据收集数据 打开电池数据获取:adb shell dumpsys batterystats --enable full-wake-history 重置电池数据: adb shell du ...
- Sobel 边缘检测算子
转自:http://blog.csdn.net/xiaqunfeng123/article/details/17302003 Sobel 算子是一个离散微分算子 (discrete different ...
- 6核 CPU导致SQL2005安装时出“无法启动服务”错
周一新买的IBM3650M3的服务器上安装SQL server2005 安装到一半时,报"提示:SQL Server 服务无法启动."错. 换了几个操作系统版本和换了几个版本的sq ...
- 关于使用python ~取反操作符带出的一系列问题
晚上的时候,无意之间看到stackoverflow上面的一个编程挑战赛,各路高手各种搞事,看到python的地方突然发现用了很多位运算的符号,但是~符号引起了我和同事的注意. 我们很少在程序中使用这种 ...
- [再寄小读者之数学篇](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, ...
- 弄懂promise
ECMAscript 6 原生提供了 Promise 对象. Promise 对象代表了未来将要发生的事件,用来传递异步操作的消息 有了 Promise 对象,就可以将异步操作以同步操作的流程表达出来 ...
- 论文阅读笔记:《Contextual String Embeddings for Sequence Labeling》
文章引起我关注的主要原因是在CoNLL03 NER的F1值超过BERT达到了93.09左右,名副其实的state-of-art.考虑到BERT训练的数据量和参数量都极大,而该文方法只用一个GPU训了一 ...
- CF1119B Alyona and a Narrow Fridge
题目地址:CF1119B Alyona and a Narrow Fridge \(O(n^2)\) 暴力枚举+贪心 从小到大枚举答案 假设枚举到 \(i\) ,将 \(a_1\) 到 \(a_i\) ...
- 【转】Python3 操作符重载方法
Python3 操作符重载方法 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/70769628 以下代码 ...
- Lua中的环境概念
[前言] Lua将其所有的全局变量保存在一个常规的table中,这个table称为“环境”.这种组织结构的优点在于,其一,不需要再为全局变量创造一种新的数据结构,因此简化了Lua的内部实现:另一个优点 ...