JAVA笔记--数组和字符串常用方法

数组

  1. 定义

    int arr[];
    int[] arr2;
    
  2. 初始化

    int arr[] = new int[]{1, 3, 5, 7, 9};
    int[] arr2 = {2, 4, 6, 8, 10};
    
  3. 数组长度length

    	int[] array = new int[5];
    	System.out.println(array.length);
    
  4. 遍历

    • 使用for循环
    • 使用foreachfor (int x: arr2) { System.out.print(x + "\t"); // 2 4 6 8 10 }
  5. 填充fill

    • 全部填充void fill(int[] a, int val)
    • 指定索引的元素填充void fill(int[] a, int fromIndex, int toIndex, int val)
       Arrays.fill(arr3, 2);
       for (int x: arr3) {
         System.out.print(x + "\t"); // 2 2 2 2 2  全部填充为2
       }
       System.out.println();
       Arrays.fill(arr3, 2, 3, 8);
       for (int x: arr3) {
         System.out.print(x + "\t"); // 2 2 8 8 2 填充指定索引
       }
    
  6. 排序sort(升序)

    • 全部排序void sort(int[] a)
    • 指定索引排序void sort(int[] a, int fromIndex, int toIndex)
       int[] arr4 = {3, 7, 2, 1, 9};
       Arrays.sort(arr4);
       for (int x: arr4) {
         System.out.print(x + "\t"); // 1 2 3 7 9
       }
       System.out.println();
       int[] arr5 = {3, 7, 2, 1, 9};
       Arrays.sort(arr5, 1, 3);
       for (int x: arr5) {
         System.out.print(x + "\t"); // 3 2 7 1 9
       }
    
  7. 复制数组copy

    • 复制数组并指定新数组长度int[] copyOf(int[] original, int newLength)
    • 指定索引范围赋值int[] copyOfRange(int[] original, int from, int to)
      int[] arr6 = {1, 2, 3, 4, 5};
      int[] arr7 = Arrays.copyOf(arr6, 5); // 1 2 3 4 5
      int[] arr8 = Arrays.copyOfRange(arr6, 1, 3); // 2 3
      for (int x: arr7) {
        System.out.print(x + "\t");
      }
      System.out.println();
      for (int x: arr8) {
        System.out.print(x + "\t");
      }
    
  8. 比较数组equals,比较两者元素值是否相等

    	public static boolean equals(long[] a, long[] a2)
    	如果两个数组以相同顺序包含相同的元素,则两个数组是相等的。
    	同样的方法适用于所有的其他基本数据类型(Byte,short,Int等)。
    
  9. 二分查找数组元素binarySearch

    	public static int binarySearch(Object[] a, Object key)
    	用二分查找算法在给定数组中搜索给定值的对象(Byte,Int,double)。
    	数组在调用前必须排序好的。
    	如果查找值包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)

字符串

  1. 字符串长度length

    String s3 = new String("Hello Java");
    System.out.println(s3.length()); // 10
    
  2. 获取字符串索引indexof

    str.indexOf(substr)//从头开始查找
    str.lastIndexOf(substr)//从尾开始查找
    String s4 = new String("how are you");
    System.out.println(s4.indexOf("o")); // 1 从头开始查找
    System.out.println(s4.lastIndexOf("o")); // 9 从尾开始查找
    
  3. 获取指定索引的字符charAt

    String s5 = new String("Hello Java");
    System.out.println(s5.charAt(4)); // o
    //java没有C++一样的用[x]输出字符串元素 !!!
    
  4. 字符串连接 +

    String s = new String("Hello");
    String s2 = new String("World");
    System.out.println(s + " " + s2); // Hello World
    
  5. 字符串判等

      String se = new String("Summer is so Hot");
      String se1 = new String("Summer is so Hot");
      String se2 = new String("summer is so hot");
      String se3 = se;
      System.out.println(se == se1); // false 比较内存而非字符串内容
      System.out.println(se == se3); // true
      System.out.println(se.equals(se1)); // true 比较字符串内容
      System.out.println(se.equals(se2)); // false
      System.out.println(se.equalsIgnoreCase(se2)); // true 忽略大小写
      System.out.println(se2.startsWith("summer")); // true 字符串开始
      System.out.println(se2.endsWith("cold")); // false 字符串结尾
    
  6. 大小写转换

      String sc = new String("hello WORLD");
      String scl = sc.toLowerCase(); // hello world 转换为小写
      String scu = sc.toUpperCase(); // HELLO WORLD 转换为大写
      System.out.println(scl + " " + scu);
    
  7. 截取字串subString

	String str;1)str=str.substring(int beginIndex);
	截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str;
	(2)str=str.substring(int beginIndex,int endIndex);
	截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str;
  1. 判断字符串中是否包含另一个字串
    boolean contains(str):
    特殊之处:indexOf(str):可以索引str第一次出现位置,如果返回-1.表示该str不在字符串中存在。
    所以,也可以用于对指定判断是否包含。
    if(str.indexOf("aa")!=-1)
    而且该方法即可以判断,也可以获取出现的位置
  1. 切割split
	String[] split(regex);//按regex进行切割
	String[] word=ss.split('\\s');
  1. 替换 replace
 String replace(oldchar,newchar);
 String replaceall(oldchar,newchar);//参数也可以为string

后续补充

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值