列表(list)
list中添加,获取,删除元素
添加方法是:.add(e);
获取方法是:.get(index); 删除方法是:.remove(index), 按照索引删除; .remove(Object o); 按照元素内容删除;import java.util.*;public class one { public static void main(String[] args) { Listperson=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) { Listfruits=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="悟空"; Listpeople=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) { Listnames=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