列表(list)

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

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

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

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

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

import java.util.*;
public class one {
public static void main(String[] args) {
List<String> person=new ArrayList<>();
person.add("jackie"); //索引为0 //.add(e)
person.add("peter"); //索引为1
person.add("annie"); //索引为2
person.add("martin"); //索引为3
person.add("marry"); //索引为4 person.remove(3); //.remove(index)
person.remove("marry"); //.remove(Object o) String per="";
per=person.get(1);
System.out.println(per); ////.get(index) for (int i = 0; i < person.size(); i++) {
System.out.println(person.get(i)); //.get(index)
}
}
}

list中是否包含某个元素

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

import java.util.*;
public class one {
public static void main(String[] args) {
List<String> fruits=new ArrayList<>();
fruits.add("苹果");
fruits.add("香蕉");
fruits.add("桃子");
//for循环遍历list
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
String appleString="苹果";
//true or false
System.out.println("fruits中是否包含苹果:"+fruits.contains(appleString)); if (fruits.contains(appleString)) {
System.out.println("我喜欢吃苹果");
}else {
System.out.println("我不开心");
}
}
}

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

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

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

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

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

.indexOf(); 和 .lastIndexOf();

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

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

StringBuffer 和 StringBuilder 类

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

public class one {
public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer("这里面可添加值:");
sBuffer.append("one");
sBuffer.append("two");
sBuffer.append("three");
System.out.println(sBuffer);
}
}

总结:

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

数组

遍历数组

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

import java.util.*;
public class one {
/**模拟学生识字 */
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入");
String input = sc.next();
char[] array = input.toCharArray();//一维数组(把输入的String转为char类型)注意是单个不能是一组数据不然会报错
for (int i = 0;i < 3;i++) {//其实无需循环就可达成
if (i == 0) {
System.out.println("老师" + "“" + input + "”" + "一个一个字 读");
for (int j = 0;j < array.length;j++) {
System.out.println("学生:" + array[j]);
}
}
else if (i == 1){
int start = 0;//申明初始取值位置
int counts = array.length;
int step_length,loop2;
if (array.length % 2 == 0) {//判断输入的数据单双
loop2 = array.length / 2;
}
else {
loop2 = array.length / 2 + 1;//如果输入的数据为单数直接除2 会省去小数导致循环次数不够所以加1
}
for (int k = 0;k < loop2;k++) {
if (counts % 2 == 0 || counts - 2 >= 1) {//判断剩余的数据下表是否够取2
step_length = 2;
}
else {//如果剩余数量不够取2那么小标为1
step_length = 1;
}
String str1 = new String(array,start,step_length);//传入对象array从array到step_length下标取值类似xx 到 xx
if(k == 0) {//老师的对白说一次
System.out.println("老师:" + "两个两个字念");
}
System.out.println("学生:" + str1);
start += 2;//增加取值初始位
counts -= 2;//剩余数据个数减少,用于上面判断取值位置
}
}
else if (i == 2) {
String str = new String(array,0,array.length);
System.out.println("老师:一整句念完");
System.out.println("学生:" + str);
}
}
sc.close();
}
}

按下标取值并判断

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

public class one {
/**识别数组中数据*/
public static void main(String[] args) {
String[] array = {"津A.123","沪A.123","京A.123"};
for (String i : array) {
char value = i.charAt(0);//String转char
String values = Character.toString(value);
if (values.equals("津")) {//equals断言values是否等于xx
System.out.println("天津");
}
if (values.equals("沪")) {
System.out.println("上海");
}
if (values.equals("京")) {
System.out.println("北京");
}
}
}
}

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

涉及StringBuilder,翻转数组

public class one {
/**判断数组中名字最后一个字重复的名字有哪些*/
public static void main(String[] args) {
String[] array = {"张三","李四","王五","赵六","周七","王哲","白浩","贾蓉","慕容阿三","黄蓉"};
StringBuilder str = new StringBuilder();//这个也可以StringBuffer str2 = new StringBuffer();
StringBuilder str2 = new StringBuilder();//存放重复的数据
StringBuilder str3 = new StringBuilder();//存放二次循环不重复的值
//首次遍历返回后面重复的姓名
for (String i : array) {
String value = i.substring(i.length() - 1,i.length());//获取i对象的最后一个字符如i为2那么value取下标1-2
int index = str.indexOf(value);//判断value是否存在str中如果不存在返回-1
if (index == -1) {//如果这个值不存在那么添加到str
str.append(value);
}
else {
str2.append(i + " ");
}
}
//翻转数组
for (int i = 0;i <= array.length / 2 - 1;i++) {
String temp1 = array[i];
String temp2 = array[array.length - i -1];
array[i] = temp2;
array[array.length - i -1] = temp1;
}
//二次遍历输出前面重复的姓名
for (String j : array) {
String value2 = j.substring(j.length() - 1,j.length());//获取i对象的最后一个字符如i为2那么value取下标1-2
int index2 = str3.indexOf(value2);
if (index2 == -1) {
str3.append(value2);
}
else {
str2.append(j + " ");
}
}
System.out.println("最后一个字重复的名字有:" + str2);
}
}

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

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

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

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

字符串

获取字符串索引位置

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

字符串排序

import java.util.*;
public class one {
/**判断字符串中是否有需要的数据*/
public static void main(String[] args) {
String str = "acd312";
char[] value = str.toCharArray();
Arrays.sort(value);
String new_str = String.copyValueOf(value);//可不写,同String new_str = new String(value);
System.out.println(new_str);
}
} 执行结果: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. koa的洋葱圈模型

    拿以下这段代码为例: const Koa = require('koa'); const app = new Koa(); // x-response-time app.use(async (ctx, ...

  2. 51job_selenium测试

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  3. 【JUC】JDK1.8源码分析之CyclicBarrier

    一.前言 有了前面分析的基础,现在,接着分析CyclicBarrier源码,CyclicBarrier类在进行多线程编程时使用很多,比如,你希望创建一组任务,它们并行执行工作,然后在进行下一个步骤之前 ...

  4. HDU - 1828 Picture

    题目链接 题意  多个矩形重叠在一起,求出轮廓线的长度. 分析  把矩形分成横线和竖线来处理.现在分析横线的,竖线同理.矩形的坐标都是整数,且范围不大,故此题不需要离散化.从下往上扫描横线,每遇到一条 ...

  5. kruskal算法:POJ No.3723 Conscription_最小生成树应用_最大权森林

    #define _CRT_SECURE_NO_WARNINGS /* 5 5 8 4 3 6831 1 3 4583 0 0 6592 0 1 3063 3 3 4975 1 3 2049 4 2 2 ...

  6. 收集服务器网卡和IP信息

    收集服务器网卡和IP信息 Python2环境 #!/usr/bin/python2 # -*- coding:utf-8 -*- import os,sys import socket, fcntl, ...

  7. 嵌入式iframe子页面与父页面js通信方式

    iframe框架中的页面与主页面之间的通信方式根据iframe中src属性是同域链接还是跨域链接,有明显不同的通信方式,同域下的数据交换和DOM元素互访就简单的多了,而跨域的则需要一些巧妙的方式来实现 ...

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

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

  9. Eclipse中项目不会自动编译问题的坑和注意点

    最近接受了几个又小有老的项目,用eclipse反而比idea方便,但是好长时间不用eclipse了,还有有些问题的! 主要是碰到了classnotfound这个难缠的问题:这里记录一下几个坑,避免以后 ...

  10. Nginx软件优化【转】

    转自 Nginx软件优化 - 惨绿少年 - 博客园 Nginx软件优化 - 惨绿少年 - 博客园 https://www.cnblogs.com/clsn/p/8484559.html 1.1 Ngi ...