当前位置:   article > 正文

Java学习笔记(9)

Java学习笔记(9)

Stringjoiner

Add添加的内容只能是字符串

如果要添加的是一个int怎么办呢?

sj.add(arr[i] + "");

+一个”” 空字符串,这样就变成了一个字符串

  1. package exercise;
  2. import java.util.StringJoiner;
  3. public class exercise11 {
  4. public static void main(String[] args) {
  5. int[] arr = {1, 2, 3, 1518, 1115};
  6. System.out.println(arrToString(arr));
  7. }
  8. public static String arrToString(int[] arr) {
  9. StringJoiner sj = new StringJoiner(", ", "[","]");
  10. String result = "";
  11. for (int i = 0; i < arr.length; i++) {
  12. sj.add(arr[i] + "");
  13. }
  14. return sj.toString();
  15. }
  16. }

字符串原理

如果没有变量

JDK8之前

+号其实也有new了个stringbuilder

所以

String s2 = s1 + “b”; 至少会new了两个对象,一个是stringbuilder对象,一个是string字符串对象(是在tostring里面new的)

JDK8做了优化

会预估最终字符串的长度,并创建数组

会扩容,默认是16 查容量(sb.capacity())

如果存的更多并超出,创建实际的长度

容量是有上限的,最大是int的最大值

  1. package exercise;
  2. import java.util.Scanner;
  3. public class exercise12 {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. String str;
  7. while (true) {
  8. System.out.println("输入字符串:");
  9. str = sc.next();
  10. if (checkStr(str)) {
  11. break;
  12. }else {
  13. System.out.println("出错,重新输!");
  14. }
  15. }
  16. StringBuilder sb = new StringBuilder();
  17. for (int i = 0; i < str.length(); i++) {
  18. sb.append(changeToLuoMa(str.charAt(i) - 48));
  19. }
  20. System.out.println(sb);
  21. }
  22. public static boolean checkStr(String str) {
  23. if (str.length() > 9) {
  24. return false;
  25. }
  26. for (int i = 0; i < str.length(); i++) {
  27. char c = str.charAt(i);
  28. if (c < '0' || c > '9') {
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34. public static String changeToLuoMa(int number) {
  35. String[] Luoma = {"", "I ", "II ", "III ", "IV ", "V ", "VI ", "VII ", "VIII ", "IX ", "X "};
  36. return Luoma[number];
  37. }
  38. }

toCharArray()

  1. package exercise;
  2. public class exercise13 {
  3. public static void main(String[] args) {
  4. String strA = "abcde";
  5. String strB = "cdecb";
  6. boolean check = check(strA, strB);
  7. if (check) {
  8. System.out.println("匹配成功!");
  9. }else {
  10. System.out.println("匹配失败!");
  11. }
  12. }
  13. public static String rotate(String str) {
  14. return str.substring(1) + str.charAt(0);
  15. }
  16. public static boolean check(String strA, String strB){
  17. for (int i = 0; i < strA.length(); i++) {
  18. strA = rotate(strA);
  19. if (strA.equals(strB)) {
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. }

  1. package exercise;
  2. import java.util.Random;
  3. public class exercise15 {
  4. public static void main(String[] args) {
  5. char[] resultChar = new char[5];
  6. char[] arr = new char[52];
  7. for (int i = 0; i < 26; i++) {
  8. arr[i] = (char) ('a' + i);
  9. arr[i + 26] = (char) ('A' + i);
  10. }
  11. Random r = new Random();
  12. for (int i = 0; i < 4; i++) {
  13. int index = r.nextInt(52);
  14. resultChar[i] = arr[index];
  15. }
  16. int number = r.nextInt(10);
  17. resultChar[4] = (char) (number + 48);
  18. int index = r.nextInt(5);
  19. char temp = resultChar[4];
  20. resultChar[4] = resultChar[index];
  21. resultChar[index] = temp;
  22. String resultStr = new String(resultChar);
  23. System.out.println(resultStr);
  24. }
  25. }

  1. package exercise;
  2. public class exercise16 {
  3. public static void main(String[] args) {
  4. String num1 = "13";
  5. String num2 = "57";
  6. //string -> int -> string
  7. int num1Int = charToInt(num1);
  8. int num2Int = charToInt(num2);
  9. String result = num1Int * num2Int + "";
  10. System.out.println(result);
  11. }
  12. public static int charToInt(String num) {
  13. char[] charArray = num.toCharArray();
  14. int result = 0;
  15. for (char c : charArray) {
  16. int ge = c - 48;
  17. result = result * 10 + ge;
  18. }
  19. return result;
  20. }
  21. }

  1. package exercise;
  2. public class exercise17 {
  3. public static void main(String[] args) {
  4. String str = " hjel sjv aesdsfsjkes ";
  5. char[] charArray = str.toCharArray();
  6. int count = 0;
  7. for (int i = charArray.length - 1; i >= 0; i--) {
  8. if (charArray[i] != 32) {
  9. count++;
  10. } else if (count != 0) {
  11. break;
  12. }
  13. }
  14. System.out.println(count);
  15. }
  16. }

集合

集合不能存基本数据类型,要变成包装类才可以存

Arraylist

集合为空,打印出来会是[]

注意:

增加功能:

Append 是stringbuilder的

Add是stringjoiner和arraylist的

获取长度:

Size()是arraylist的一个方法,所以带一个()

arraylist获取其中的元素是通过get(索引)得到的,arraylist[索引]是得不到的,因为arraylist里面存的是地址

基本数据类型对应的包装类 写在<>里

返回多个数据,最好用集合返回

  1. package exercise;
  2. import java.util.ArrayList;
  3. public class exercise2 {
  4. public static void main(String[] args) {
  5. ArrayList<Phone> phoneArr = new ArrayList<>();
  6. Phone phone1 = new Phone("xiaomi", 1000);
  7. Phone phone2 = new Phone("apple", 8000);
  8. Phone phone3 = new Phone("hammer", 2999);
  9. phoneArr.add(phone1);
  10. phoneArr.add(phone2);
  11. phoneArr.add(phone3);
  12. ArrayList<Phone> phoneInfoList = getPhoneInfo(phoneArr);
  13. for (int i = 0; i < phoneInfoList.size(); i++) {
  14. Phone phone = phoneInfoList.get(i);
  15. System.out.println(phone.getBrand() + ", " + phone.getPrice());
  16. }
  17. }
  18. public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> phoneArr) {
  19. ArrayList<Phone> result = new ArrayList<>();
  20. for (Phone phone : phoneArr) {
  21. if (phone.getPrice() < 3000) {
  22. result.add(phone);
  23. }
  24. }
  25. return result;
  26. }
  27. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/244241
推荐阅读
相关标签
  

闽ICP备14008679号