赞
踩
Stringjoiner
Add添加的内容只能是字符串
如果要添加的是一个int怎么办呢?
sj.add(arr[i] + "");
+一个”” 空字符串,这样就变成了一个字符串
- package exercise;
-
- import java.util.StringJoiner;
-
- public class exercise11 {
- public static void main(String[] args) {
- int[] arr = {1, 2, 3, 1518, 1115};
- System.out.println(arrToString(arr));
- }
-
- public static String arrToString(int[] arr) {
- StringJoiner sj = new StringJoiner(", ", "[","]");
- String result = "";
- for (int i = 0; i < arr.length; i++) {
- sj.add(arr[i] + "");
- }
- return sj.toString();
- }
- }

字符串原理
如果没有变量
JDK8之前
+号其实也有new了个stringbuilder
所以
String s2 = s1 + “b”; 至少会new了两个对象,一个是stringbuilder对象,一个是string字符串对象(是在tostring里面new的)
JDK8做了优化
会预估最终字符串的长度,并创建数组
会扩容,默认是16 查容量(sb.capacity())
如果存的更多并超出,创建实际的长度
容量是有上限的,最大是int的最大值
- package exercise;
-
- import java.util.Scanner;
-
- public class exercise12 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String str;
- while (true) {
- System.out.println("输入字符串:");
- str = sc.next();
- if (checkStr(str)) {
- break;
- }else {
- System.out.println("出错,重新输!");
- }
- }
-
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < str.length(); i++) {
- sb.append(changeToLuoMa(str.charAt(i) - 48));
- }
- System.out.println(sb);
-
- }
-
- public static boolean checkStr(String str) {
- if (str.length() > 9) {
- return false;
- }
-
- for (int i = 0; i < str.length(); i++) {
- char c = str.charAt(i);
- if (c < '0' || c > '9') {
- return false;
- }
- }
-
- return true;
- }
-
- public static String changeToLuoMa(int number) {
- String[] Luoma = {"", "I ", "II ", "III ", "IV ", "V ", "VI ", "VII ", "VIII ", "IX ", "X "};
- return Luoma[number];
- }
- }

toCharArray()
- package exercise;
-
- public class exercise13 {
- public static void main(String[] args) {
- String strA = "abcde";
- String strB = "cdecb";
-
- boolean check = check(strA, strB);
- if (check) {
- System.out.println("匹配成功!");
- }else {
- System.out.println("匹配失败!");
- }
-
-
- }
-
- public static String rotate(String str) {
- return str.substring(1) + str.charAt(0);
- }
-
- public static boolean check(String strA, String strB){
- for (int i = 0; i < strA.length(); i++) {
- strA = rotate(strA);
- if (strA.equals(strB)) {
- return true;
- }
- }
- return false;
- }
- }

- package exercise;
-
- import java.util.Random;
-
- public class exercise15 {
- public static void main(String[] args) {
- char[] resultChar = new char[5];
-
- char[] arr = new char[52];
- for (int i = 0; i < 26; i++) {
- arr[i] = (char) ('a' + i);
- arr[i + 26] = (char) ('A' + i);
- }
-
- Random r = new Random();
- for (int i = 0; i < 4; i++) {
- int index = r.nextInt(52);
- resultChar[i] = arr[index];
- }
-
- int number = r.nextInt(10);
- resultChar[4] = (char) (number + 48);
-
- int index = r.nextInt(5);
- char temp = resultChar[4];
- resultChar[4] = resultChar[index];
- resultChar[index] = temp;
-
- String resultStr = new String(resultChar);
-
- System.out.println(resultStr);
-
-
- }
- }

- package exercise;
-
- public class exercise16 {
- public static void main(String[] args) {
- String num1 = "13";
- String num2 = "57";
-
- //string -> int -> string
- int num1Int = charToInt(num1);
- int num2Int = charToInt(num2);
-
- String result = num1Int * num2Int + "";
-
- System.out.println(result);
- }
-
- public static int charToInt(String num) {
- char[] charArray = num.toCharArray();
- int result = 0;
- for (char c : charArray) {
- int ge = c - 48;
- result = result * 10 + ge;
- }
- return result;
- }
-
-
- }

- package exercise;
-
- public class exercise17 {
- public static void main(String[] args) {
- String str = " hjel sjv aesdsfsjkes ";
- char[] charArray = str.toCharArray();
- int count = 0;
- for (int i = charArray.length - 1; i >= 0; i--) {
-
- if (charArray[i] != 32) {
- count++;
- } else if (count != 0) {
- break;
- }
- }
- System.out.println(count);
- }
- }

集合
集合不能存基本数据类型,要变成包装类才可以存
Arraylist
集合为空,打印出来会是[]
注意:
增加功能:
Append 是stringbuilder的
Add是stringjoiner和arraylist的
获取长度:
Size()是arraylist的一个方法,所以带一个()
arraylist获取其中的元素是通过get(索引)得到的,arraylist[索引]是得不到的,因为arraylist里面存的是地址
基本数据类型对应的包装类 写在<>里
返回多个数据,最好用集合返回
- package exercise;
-
- import java.util.ArrayList;
-
- public class exercise2 {
- public static void main(String[] args) {
- ArrayList<Phone> phoneArr = new ArrayList<>();
-
- Phone phone1 = new Phone("xiaomi", 1000);
- Phone phone2 = new Phone("apple", 8000);
- Phone phone3 = new Phone("hammer", 2999);
-
- phoneArr.add(phone1);
- phoneArr.add(phone2);
- phoneArr.add(phone3);
-
- ArrayList<Phone> phoneInfoList = getPhoneInfo(phoneArr);
- for (int i = 0; i < phoneInfoList.size(); i++) {
- Phone phone = phoneInfoList.get(i);
- System.out.println(phone.getBrand() + ", " + phone.getPrice());
- }
- }
-
- public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> phoneArr) {
- ArrayList<Phone> result = new ArrayList<>();
-
- for (Phone phone : phoneArr) {
- if (phone.getPrice() < 3000) {
- result.add(phone);
- }
- }
- return result;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。