列表(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. OS + Linux RedHat 6 / redhat 6 configuration / configure / autoconf / make / make install

    s These critical programs are missing or too old: as ld http://blog.csdn.net/testcs_dn/article/detai ...

  2. MYSQL增加tmp_table_size 的操作

    最近有张表经常损坏,修复后还是会出现损坏. dba分析有可能是临时表空间太小导致的.以下是设置临时表空间大小的操作. 设置 tmp_table_size的大小 mysql> set global ...

  3. SQL连接服务器链接失败

    链接服务器"AGPSServer"的 OLE DB 访问接口 "SQLNCLI10" 返回了消息 "登录超时已过期".链接服务器" ...

  4. 附录B. Spring Boot 配置文件application.properties

    #SPRING CONFIG (ConfigFileApplicationListener) spring.config.name= # config file name (default to 'a ...

  5. Hbase记录-ZooKeeper介绍

    ZooKeeper是一个分布式协调服务来管理大量的主机.协调和管理在分布式环境的一个服务是一个复杂的过程.ZooKeeper 简单解决了其结构和API这个问题.ZooKeeper允许开发人员能够专注于 ...

  6. golang数组声明

    格式 初始化数组 {}中的元素数不能大于[]中的数字,并且长度在初始化后不能改变,定义数组时需指定长度 ... var arrName [num]type = [num]type{value, val ...

  7. eclipse导入项目后找不到.class文件

    今天从git上clone代码到eclipse中,发现项目->右键没有java build path选项,而且src下没有包路径,都显示成文件夹. 发现项目中没有.class文件 后来发现项目-& ...

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

  9. 表格重新加载 where 携带上次值问题

    表格重载两种方式: 方式一: tableIns.reload(options)   注意这种方式的重载是不会携带上次数据加载时的where值     //使用 第一次渲染返回的对象 var table ...

  10. Netty入门(4) - 附带的ChannelHandler和Codec

    使用SSL/TLS创建安全的Netty程序 Java提供了抽象的SslContext和SslEngine,实际上SslContext可以用来获取SslEngine来进行加密和解密.Netty拓展了Ja ...