Day_11【集合】扩展案例5_对list集合对象中的元素进行反转,求最大值最小值,求元素i在list集合中首次出现的索引,将oldvalue替换为newvalue
分析以下需求,并用代码实现
定义MyArrays工具类,该工具类中有以下方法,方法描述如下: 1.public static void reverse(ArrayList<Integer> list);
参数ArrayList<Integer> list:要进行操作的集合对象
要求:对list集合对象中的元素进行反转
(第一个和最后一个交换,第二个和倒数第二个交换,第三个和倒数第三个交换...) 2.public static Integer max(ArrayList<Integer> list);
参数ArrayList<Integer> list:要进行操作的集合对象
要求:求出list集合对象中的最大值并返回 3.public static Integer min(ArrayList<Integer> list);
参数ArrayList<Integer> list:要进行操作的集合对象
要求:求出list集合对象中的最小值并返回 4.public static int indexOf(ArrayList<Integer> list,Integer i);
参数ArrayList<Integer> list:要进行操作的集合对象
参数Integer i:需要在集合中查找的元素
要求:求出元素i在list集合中第一次出现的索引,如果没有返回-1 5.public static void replaceAll(ArrayList<Integer> list,Integer oldValue,Integer newValue);
参数ArrayList<Integer> list:要进行操作的集合对象
参数Integer oldValue:需要被替换掉的原值
参数Integer newValue:替换后的新值
要求:将list集合中的所有值为oldValue的元素替换为newValue
代码
package com.itheima5;
import java.util.ArrayList;
public class MyArrays {
/*
* public static void reverse(ArrayList<Integer> list);
* 参数ArrayList<Integer> list:要进行操作的集合对象
* 要求:对list集合对象中的元素进行反转
* (第一个和最后一个交换,第二个和倒数第二个交换,第三个和倒数第三个交换...)
*/
public static void reverse(ArrayList<Integer> list) {
for(int start = 0,end = list.size() - 1;start < end;start++,end--) {
Integer in = list.get(start);
list.set(start, list.get(end));
list.set(end, in);
}
}
/*
* public static Integer max(ArrayList<Integer> list);
* 参数ArrayList<Integer> list:要进行操作的集合对象
* 要求:求出list集合对象中的最大值并返回
*/
public static Integer max(ArrayList<Integer> list) {
Integer max = list.get(0);
for(int i = 0;i < list.size();i++) {
if(list.get(i) > max) {
max = list.get(i);
}
}
return max;
}
/*
* public static Integer min(ArrayList<Integer> list);
* 参数ArrayList<Integer> list:要进行操作的集合对象
* 要求:求出list集合对象中的最小值并返回
*/
public static Integer min(ArrayList<Integer> list) {
Integer min = list.get(0);
for(int i = 0;i < list.size();i++) {
if(list.get(i) < min) {
min = list.get(i);
}
}
return min;
}
/*
* public static int indexOf(ArrayList<Integer> list,Integer i);
* 参数ArrayList<Integer> list:要进行操作的集合对象
* 参数Integer i:需要在集合中查找的元素
* 要求:求出元素i在list集合中第一次出现的索引,如果没有返回-1
*/
public static int indexof(ArrayList<Integer> list,Integer i) {
return list.indexOf(i);
}
/*
* public static void replaceAll(ArrayList<Integer> list,Integer oldValue,Integer newValue);
* 参数ArrayList<Integer> list:要进行操作的集合对象
* 参数Integer oldValue:需要被替换掉的原值
* 参数Integer newValue:替换后的新值
* 要求:将list集合中的所有值为oldValue的元素替换为newValue
*/
public static void replaceAll(ArrayList<Integer> list,Integer oldValue,Integer newValue) {
for(int i = 0;i < list.size();i++) {
if(list.get(i).equals(oldValue)) {
list.set(i, newValue);
}
}
}
}
package com.itheima5;
import java.util.ArrayList;
public class ToolTest {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(123);
list.add(234);
list.add(345);
list.add(543);
list.add(432);
list.add(321);
MyArrays ma = new MyArrays();
ma.reverse(list);
System.out.println(list);
System.out.println(ma.max(list));
System.out.println(ma.min(list));
Integer i = 432;
System.out.println(ma.indexof(list, i));
Integer oldValue = 321;
Integer newValue = 111;
ma.replaceAll(list, oldValue, newValue);
System.out.println(list);
}
}
控制台输出内容

Day_11【集合】扩展案例5_对list集合对象中的元素进行反转,求最大值最小值,求元素i在list集合中首次出现的索引,将oldvalue替换为newvalue的更多相关文章
- Day_09【常用API】扩展案例5_获取长度为5的随机字符串,字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成
分析以下需求,并用代码实现 1.定义String getStr(char[] chs)方法 功能描述:获取长度为5的随机字符串,字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成 ...
- elasticsearch聚合案例--分组、求最大值再求最大值的均值
一.需求 A.B.C代表3个用户,第二列代表各自的得分,求A.B.C的最好成绩以及A.B.C最好成绩的均值 A 10 A 11 A 13 B 11 B 11 B 12 C 10 C 10 C 11 C ...
- Day_11【集合】扩展案例2_使用普通for循环获取集合中索引为3的元素并打印,统计集合中包含字符串"def"的数量,删除集合中的所有字符串",将集合中每个元素中的小写字母变成大写字母def",
分析以下需求,并用代码实现 1.定义ArrayList集合,存入多个字符串"abc" "def" "efg" "def" ...
- Day_12【集合】扩展案例1_利用集合的知识对长度为10的int数组进行去重,产生新数组,不能改变数组中原来数字的大小顺序
分析以下需求,并用代码实现 1.定义一个长度为10的int数组,并存入10个int类型的数据,其中有一些数据是重复的 2.利用集合的知识对数组进行去重,产生新数组,不能改变数组中原来数字的大小顺序 3 ...
- 《java入门第一季》之ArrayList集合小案例
案例一:去除集合里面的重复元素 package cn.itcast_04; import java.util.ArrayList; /* * ArrayList去除集合中字符串的重复值(字符串的内容相 ...
- 006-guava 集合-集合工具类-集合扩展工具类
一.概述 需要实现自己的集合扩展.也许你想要在元素被添加到列表时增加特定的行为,或者你想实现一个Iterable,其底层实际上是遍历数据库查询的结果集 二.使用 2.1.ForwardingList装 ...
- 浅谈集合框架六——集合扩展:Arrays工具类、集合与数组相互转换方式;
最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出.初学者对于本篇博客只建议作为参考,欢迎留言共同学习. 之前有介绍集合框架的体系 ...
- 浅谈集合框架四——集合扩展:集合循环输出方式及list输出方式的效率对比
最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出.初学者对于本篇博客只建议作为参考,欢迎留言共同学习. 之前有介绍集合框架的体系 ...
- 【Java】集合综合案例 - 播放器管理
集合综合案例 文章目录 集合综合案例 需求分析 项目演示 详细设计 代码实现 歌曲类 播放器类 播放列表类 测试 参考资料 播放器管理 需求分析 项目演示 详细设计 代码实现 重新搞一波 复习巩固 简 ...
随机推荐
- L24 word2vec
词嵌入基础 我们在"循环神经网络的从零开始实现"一节中使用 one-hot 向量表示单词,虽然它们构造起来很容易,但通常并不是一个好选择.一个主要的原因是,one-hot 词向量无 ...
- Ignatius and the Princess IV HDU 1029
题目大意: n个数字,找出其中至少出现(n+1)/2次的数字,并且保证n是奇数. 题解:这道题数组是不能用的,因为题目没有明确输入的数据范围,比如输入了一个1e9,数组肯定开不了这么大.所以要用map ...
- Cyclic Nacklace 杭电3746
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- F - Minimum Sum LCM
LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multip ...
- mapstruct使用详解
我们都知道,随着一个工程的越来越成熟,模块划分会越来越细,其中实体类一般存于 domain 之中,但 domain 工程最好不要被其他工程依赖,所以其他工程想获取实体类数据时就需要在各自工程写 mod ...
- MRCTF Ezpop_Revenge小记
前言 一道typecho1.2的反序列化,顺便记录一下踩的坑 www.zip获得源码,结构大致如下 flag.php需要ssrf,如果成功会写入session 拿到源码直接去网上先找了一下有没有现成的 ...
- python 工具链 虚拟环境和包管理工具 pipenv
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, ...
- 移植seetafaceengine-master、opencv到ARM板
0.前言 在要移植opecv和SeetaFaceEngine-master到ARM板子上运行的所有步骤之前,有几点需要注意的: 查看板子运行的Kernel版本 交叉编译工具链的gcc版本,关键就是工具 ...
- Java 动态编译--DynamicCompiler
java 动态编译自己写过程的机会比较少,记录一下: package com.xzlf.dynamicCompile; import java.io.IOException; import java. ...
- jeecg ant design vue 一些收藏
1关于 进来清除上次记录 找到src/permission.js下的