列表(list)

list中添加,获取,删除元素

添加方法是:.add(e);  

获取方法是:.get(index);  

删除方法是:.remove(index), 按照索引删除;  

.remove(Object o); 按照元素内容删除;

  1. import java.util.*;
  2. public class one {
  3. public static void main(String[] args) {
  4. List<String> person=new ArrayList<>();
  5. person.add("jackie"); //索引为0 //.add(e)
  6. person.add("peter"); //索引为1
  7. person.add("annie"); //索引为2
  8. person.add("martin"); //索引为3
  9. person.add("marry"); //索引为4
  10. person.remove(3); //.remove(index)
  11. person.remove("marry"); //.remove(Object o)
  12. String per="";
  13. per=person.get(1);
  14. System.out.println(per); ////.get(index)
  15. for (int i = 0; i < person.size(); i++) {
  16. System.out.println(person.get(i)); //.get(index)
  17. }
  18. }
  19. }

list中是否包含某个元素

方法:.contains(Object o),返回true或者false

  1. import java.util.*;
  2. public class one {
  3. public static void main(String[] args) {
  4. List<String> fruits=new ArrayList<>();
  5. fruits.add("苹果");
  6. fruits.add("香蕉");
  7. fruits.add("桃子");
  8. //for循环遍历list
  9. for (int i = 0; i < fruits.size(); i++) {
  10. System.out.println(fruits.get(i));
  11. }
  12. String appleString="苹果";
  13. //true or false
  14. System.out.println("fruits中是否包含苹果:"+fruits.contains(appleString));
  15. if (fruits.contains(appleString)) {
  16. System.out.println("我喜欢吃苹果");
  17. }else {
  18. System.out.println("我不开心");
  19. }
  20. }
  21. }

list中根据索引将元素数值改变(替换)

set(index, element); 和 .add(index, element);

注意:他们不一样有区别的

  1. import java.util.*;
  2. public class one {
  3. public static void main(String[] args) {
  4. String a="白龙马", b="沙和尚", c="八戒", d="唐僧", e="悟空";
  5. List<String> people=new ArrayList<>();
  6. people.add(a);
  7. people.add(b);
  8. people.add(c);
  9. people.set(0, d); //.set(index, element); //将d唐僧放到list中索引为0的位置,替换a白龙马
  10. people.add(1, e); //.add(index, element); //将e悟空放到list中索引为1的位置,原来位置的b沙和尚后移一位
  11. //增j加for循环遍历list
  12. for(String str:people){
  13. System.out.println(str);
  14. }
  15. }
  16. }

list中查看(判断)元素的索引

.indexOf(); 和 .lastIndexOf();

注意:他们不一样有区别的

  1. import java.util.*;
  2. public class one {
  3. public static void main(String[] args) {
  4. List<String> names=new ArrayList<>();
  5. names.add("刘备"); //索引为0
  6. names.add("关羽"); //索引为1
  7. names.add("张飞"); //索引为2
  8. names.add("刘备"); //索引为3
  9. names.add("张飞"); //索引为4
  10. System.out.println(names.indexOf("刘备"));
  11. System.out.println(names.lastIndexOf("刘备"));
  12. System.out.println(names.indexOf("张飞"));
  13. System.out.println(names.lastIndexOf("张飞"));
  14. }
  15. }

StringBuffer 和 StringBuilder 类

由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。

  1. public class one {
  2. public static void main(String[] args) {
  3. StringBuffer sBuffer = new StringBuffer("这里面可添加值:");
  4. sBuffer.append("one");
  5. sBuffer.append("two");
  6. sBuffer.append("three");
  7. System.out.println(sBuffer);
  8. }
  9. }

总结:

  1. StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
  2. StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况

数组

遍历数组

涉及数组遍历,String字符串转char类型数组,获取数组长度array.length

  1. import java.util.*;
  2. public class one {
  3. /**模拟学生识字 */
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. System.out.println("输入");
  7. String input = sc.next();
  8. char[] array = input.toCharArray();//一维数组(把输入的String转为char类型)注意是单个不能是一组数据不然会报错
  9. for (int i = 0;i < 3;i++) {//其实无需循环就可达成
  10. if (i == 0) {
  11. System.out.println("老师" + "“" + input + "”" + "一个一个字 读");
  12. for (int j = 0;j < array.length;j++) {
  13. System.out.println("学生:" + array[j]);
  14. }
  15. }
  16. else if (i == 1){
  17. int start = 0;//申明初始取值位置
  18. int counts = array.length;
  19. int step_length,loop2;
  20. if (array.length % 2 == 0) {//判断输入的数据单双
  21. loop2 = array.length / 2;
  22. }
  23. else {
  24. loop2 = array.length / 2 + 1;//如果输入的数据为单数直接除2 会省去小数导致循环次数不够所以加1
  25. }
  26. for (int k = 0;k < loop2;k++) {
  27. if (counts % 2 == 0 || counts - 2 >= 1) {//判断剩余的数据下表是否够取2
  28. step_length = 2;
  29. }
  30. else {//如果剩余数量不够取2那么小标为1
  31. step_length = 1;
  32. }
  33. String str1 = new String(array,start,step_length);//传入对象array从array到step_length下标取值类似xx 到 xx
  34. if(k == 0) {//老师的对白说一次
  35. System.out.println("老师:" + "两个两个字念");
  36. }
  37. System.out.println("学生:" + str1);
  38. start += 2;//增加取值初始位
  39. counts -= 2;//剩余数据个数减少,用于上面判断取值位置
  40. }
  41. }
  42. else if (i == 2) {
  43. String str = new String(array,0,array.length);
  44. System.out.println("老师:一整句念完");
  45. System.out.println("学生:" + str);
  46. }
  47. }
  48. sc.close();
  49. }
  50. }

按下标取值并判断

涉及识别数组内数据,数组按下标取值,String数组转char,char转String,equals字符串断言

  1. public class one {
  2. /**识别数组中数据*/
  3. public static void main(String[] args) {
  4. String[] array = {"津A.123","沪A.123","京A.123"};
  5. for (String i : array) {
  6. char value = i.charAt(0);//String转char
  7. String values = Character.toString(value);
  8. if (values.equals("津")) {//equals断言values是否等于xx
  9. System.out.println("天津");
  10. }
  11. if (values.equals("沪")) {
  12. System.out.println("上海");
  13. }
  14. if (values.equals("京")) {
  15. System.out.println("北京");
  16. }
  17. }
  18. }
  19. }

判断数组中的数据是否重复

涉及StringBuilder,翻转数组

  1. public class one {
  2. /**判断数组中名字最后一个字重复的名字有哪些*/
  3. public static void main(String[] args) {
  4. String[] array = {"张三","李四","王五","赵六","周七","王哲","白浩","贾蓉","慕容阿三","黄蓉"};
  5. StringBuilder str = new StringBuilder();//这个也可以StringBuffer str2 = new StringBuffer();
  6. StringBuilder str2 = new StringBuilder();//存放重复的数据
  7. StringBuilder str3 = new StringBuilder();//存放二次循环不重复的值
  8. //首次遍历返回后面重复的姓名
  9. for (String i : array) {
  10. String value = i.substring(i.length() - 1,i.length());//获取i对象的最后一个字符如i为2那么value取下标1-2
  11. int index = str.indexOf(value);//判断value是否存在str中如果不存在返回-1
  12. if (index == -1) {//如果这个值不存在那么添加到str
  13. str.append(value);
  14. }
  15. else {
  16. str2.append(i + " ");
  17. }
  18. }
  19. //翻转数组
  20. for (int i = 0;i <= array.length / 2 - 1;i++) {
  21. String temp1 = array[i];
  22. String temp2 = array[array.length - i -1];
  23. array[i] = temp2;
  24. array[array.length - i -1] = temp1;
  25. }
  26. //二次遍历输出前面重复的姓名
  27. for (String j : array) {
  28. String value2 = j.substring(j.length() - 1,j.length());//获取i对象的最后一个字符如i为2那么value取下标1-2
  29. int index2 = str3.indexOf(value2);
  30. if (index2 == -1) {
  31. str3.append(value2);
  32. }
  33. else {
  34. str2.append(j + " ");
  35. }
  36. }
  37. System.out.println("最后一个字重复的名字有:" + str2);
  38. }
  39. }

判断数组数据是否以xx开始

  1. public class one {
  2. /**判断数组中提取的数据值是否以xx开始,有几个*/
  3. public static void main(String[] args) {
  4. String[] array = {"海尔冰箱","美的洗衣机","海尔洗衣机",};
  5. int sum = 0;
  6. for (String i : array) {
  7. if (i.startsWith("海尔")) {
  8. sum++;
  9. }
  10. }
  11. System.out.println("海尔共有" + sum + "个产品");
  12. }
  13. }

判断数组数据是否以xx结尾

  1. public class one {
  2. /**判断数组中提取的数据值是否以xx开始,有几个*/
  3. public static void main(String[] args) {
  4. String[] array = {"海尔冰箱","美的洗衣机","海尔洗衣机",};
  5. int sum = 0;
  6. for (String i : array) {
  7. if (i.endsWith("冰箱")) {
  8. sum++;
  9. }
  10. }
  11. System.out.println("海尔共有" + sum + "个产品");
  12. }
  13. }

字符串

获取字符串索引位置

  1. public class one {
  2. /**判断字符串中是否有需要的数据*/
  3. public static void main(String[] args) {
  4. String str = "一二三四五,上山打老虎";
  5. int value = str.indexOf(",");//indexOf获取字符串的索引位置,如果不存在返回-1
  6. if (value != -1) {
  7. System.out.println("字符串索引为:" + value);
  8. }
  9. else {
  10. System.out.println("没有需要的字符");
  11. }
  12. }
  13. }

字符串排序

  1. import java.util.*;
  2. public class one {
  3. /**判断字符串中是否有需要的数据*/
  4. public static void main(String[] args) {
  5. String str = "acd312";
  6. char[] value = str.toCharArray();
  7. Arrays.sort(value);
  8. String new_str = String.copyValueOf(value);//可不写,同String new_str = new String(value);
  9. System.out.println(new_str);
  10. }
  11. }
  12. 执行结果:123acd

Java列表、数组、字符串的更多相关文章

  1. java将前端的json数组字符串转换为列表

    记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表. 前端数据转化与请求 var contracts = [ {id: '1', name: 'yanggb合同1'}, {i ...

  2. JSon_零基础_007_将JSon格式的"数组"字符串转换为Java对象"数组"

    将JSon格式的"数组"字符串转换为Java对象"数组". 应用此技术从一个json对象字符串格式中得到一个java对应的对象. JSONObject是一个“n ...

  3. Java解析json字符串和json数组

    Java解析json字符串和json数组 public static Map<String, String> getUploadTransactions(String json){ Map ...

  4. java中数组、集合、字符串之间的转换,以及用加强for循环遍历

    java中数组.集合.字符串之间的转换,以及用加强for循环遍历: @Test public void testDemo5() { ArrayList<String> list = new ...

  5. java字符数组char[]和字符串String之间的转换

    java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...

  6. java中如何使用列表数组

    java中如何使用列表数组 觉得有用的话,欢迎一起讨论相互学习~Follow Me 转载链接 https://blog.csdn.net/hgtjcxy/article/details/8183519 ...

  7. 【java工具类】对字节数组字符串进行Base64解码并生成图片

    import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import org.springfra ...

  8. Java对数组和列表的排序1.8新特性

    Java对数组列表的排序 数组 Integer[] a = new Integer[] { 1, 2, 3, 4, 5, 6, 9, 8, 7, 4, 5, 5, 6, 6 }; Arrays.sor ...

  9. python的列表和java的数组有何异同

    今天面试被问到,自己学习一下. python的列表是可变长的,定义时不需要指定长度:pyhton是弱对象类型,python的列表存储的数据类型可以不相同:python的列表更加灵活,如可以通过''命令 ...

  10. Java列表

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

随机推荐

  1. 系统自带的日志管理工具-rsyslogd

    系统自带的日志管理工具-rsyslogd 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志管理简介 1.什么是日志 系统日志是记录系统中硬件.软件和系统问题的信息,同时还可以 ...

  2. Linux 文件管理权限

    这里呢我们需要掌握的是两个命令 chmod 和 chgrp和chown 视频地址:http://www.tudou.com/listplay/pA16IH7T_Sc/LuyHbR_7Yp0.html ...

  3. Hadoop记录-MRv2(Yarn)运行机制

    1.MRv2结构—Yarn模式运行机制 Client---客户端提交任务 ResourceManager---资源管理 ---Scheduler调度器-资源分配Containers ----在Yarn ...

  4. Study 3 —— 表格

    表格基本格式: <table> <tr> <td></td> <td></td> </tr> <tr> ...

  5. bzoj千题计划305:bzoj2565: 最长双回文串(回文自动机)

    https://www.lydsy.com/JudgeOnline/problem.php?id=2565 正着构造回文自动机 倒过来再构造一个回文自动机 分别求出以位置i开始的和结尾的最长回文串 # ...

  6. git删除仓库的某个文件

    可以用git rm命令删除文件(删除远程仓库文件) git clone 仓库地址 git add . git rm 文件//本地中该文件会被删除 git rm -r文件夹 //删除文件夹 上面会把对应 ...

  7. Ansi与Unicode编码

    视频教程:Ansi与Unicode编码 大家在编程时经常遇到的数据类型: ● Ansi: char   代表一个字符  (CHAR) char *  代表一个字符串指针   (PCHAR    PST ...

  8. POJ 2503 Babelfish (STL)

    题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...

  9. Django学习手册 - 模板继承与导入

    核心: PS:一个页面只能继承一个模板. 前置: 配置url. 配置views 关键字: 1. {% extends "index模板.html" %} 声明继承于哪个模板 ,关联 ...

  10. shiroWeb项目-认证及MD5认证信息在页面显示(十)

    realm设置完整认证信息 // realm的认证方法,从数据库查询用户信息 @Override protected AuthenticationInfo doGetAuthenticationInf ...