java常见的一些方法(一)
Java中常见的一些方法的使用:
Java to CharArray()方法
该方法将字符串转化为字符数组
语法 :
public char[] toCharArray()返回值是字符数组
示例:
public class Test {
    public static void main(String args[]) {
        String Str = new String("www.runoob.com");
        System.out.print("返回值 :" );
        System.out.println( Str.toCharArray() );
    }
}
结果: 返回值 :www.runoob.com
Java substring() 方法
返回字符串的子字符串
语法:
public String substring(int beginIndex) 或 public String substring(int beginIndex, int endIndex)
参数:
- beginIndex -- 起始索引(包括), 索引从 0 开始。
- endIndex -- 结束索引(不包括)。
返回值:  子字符串

示例:
public class RunoobTest {
    public static void main(String args[]) {
        String Str = new String("This is text");
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4) );
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4, 10) );
    }
}
结果:
返回值 : is text
返回值 : is te
Java ArrayList sort() 方法
sort()根据指定的顺序对动态数组中的元素进行排序
该方法的语法结构为:
arraylist.sort(Comparator c)注: arraylist是ArraysList类的一个对象
参数Comparator是顺序方式
sort方法不返回任何值,他只是更改动态数组中的元素的顺序
Java8 Comparator 排序方法
Java8 中 Comparator 接口提供了一些静态方法,可以方便于我们进行排序操作,下面通过例子讲解下如何使用
对整数列表排序(升序)
Comparator.naturalOrder()
List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8);
list.sort(Comparator.naturalOrder());
System.out.println(list);
对整数列表排序(降序)
- Comparator.reverseOrder()
List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8);
list.sort(Comparator.reverseOrder());
System.out.println(list);
根据对象属性(年龄)
-  Comparator.comparingInt(Person::getAge)
public class Test {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("a", 2));
        personList.add(new Person("b", 4));
        personList.add(new Person("c", 7));
        // 升序
        personList.sort(Comparator.comparingInt(Person::getAge));
        // 降序
        personList.sort(Comparator.comparingInt(Person::getAge).reversed());
        System.out.println(personList);
    }
    public static class Person {
        private String name;
        private Integer age;
        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
        public Integer getAge() {
            return age;
        }
        
        // ... toString 方法
    }
}
根据对象属性(价格、速度)
- ,需要注意的是,排序有先后之分,不同的顺序会导致不同的结果
public class Test {
    public static void main(String[] args) {
        List<Computer> list = new ArrayList<>();
        list.add(new Computer("xiaomi",4000,6));
        list.add(new Computer("sony",5000,4));
        list.add(new Computer("dell",4000,5));
        list.add(new Computer("mac",6000,8));
        list.add(new Computer("micro",5000,6));
        // 先以价格(升序)、后再速度(升序)
   list.sort(Comparator.comparingInt(Computer::getPrice).thenComparingInt(Computer::getSpeed));
        // 先以速度(降序)、后再价格(升序)
     list.sort(Comparator.comparingInt(Computer::getSpeed).reversed().thenComparingInt(Computer::getPrice));
        // 先以价格(降序)、后再速度(降序)
        list.sort(Comparator.comparingInt(Computer::getPrice).thenComparingInt(Computer::getSpeed).reversed());
        System.out.println(list);
    }
    public static class Computer {
        private String name;
        private Integer price;
        private Integer speed;
        public Computer(String name, Integer price, Integer speed) {
            this.name = name;
            this.price = price;
            this.speed = speed;
        }
        public Integer getPrice() {
            return price;
        }
        public void setPrice(Integer price) {
            this.price = price;
        }
        public Integer getSpeed() {
            return speed;
        }
        public void setSpeed(Integer speed) {
            this.speed = speed;
        }
        // ... toString 方法
    }
}
Java Math.max()方法
作用:用于返回两个参数中的最大值
语法
double max(double arg1, double arg2)
或
float max(float arg1, float arg2)
或
int max(int arg1, int arg2)
或
long max(long arg1, long arg2)
参数说明
arg1	一个 int 、 long 、 float 、 double 类型的数值
arg2	类型跟 arg1 一样的数值
Java String contains()方法
contains()方法用于判断字符串中是否包含指定的字符或者是字符串
语法:
public boolean contains(CharSquence chars)
- chars —–表示要判断的字符或者字符串
示例:
public class Main {
    public static void main(String[] args) {
        String myStr = "Runoob";
        System.out.println(myStr.contains("Run"));
        System.out.println(myStr.contains("o"));
        System.out.println(myStr.contains("s"));
    }
}
Java charAt()方法
用于返回指定搜索处的字符。索引范围从 0 到 length() - 1
语法:
public char charAt(int index) //index 指的是字符的索引
返回值: 返回指定索引处的字符
示例:
public class Test {
    public static void main(String args[]) {
        String s = "www.runoob.com";
        char result = s.charAt(6);
        System.out.println(result);
    }
}
integer的MIN_VALUE
- 在JDK中,整型类型是有范围的 -2147483648~2147483647 ( -2^31 --- 2^31-1)
- 最大值为Integer.MAX_VALUE,即2147483647,最小值为Integer.MIN_VALUE -2147483648。
- 对整形最大值加1,2147483648(越界了),那么此时值为多少呢?结果是-2147483648,即是Integer.MIN_VALUE。
- 类似的,对Integer.MIN_VALUE取反或者取绝对值呢?仍为Integer.MIN_VALUE,因为值为-2147483648,绝对值2147483648超过Integer.MAX_VALUE 2147483647。
Integer.MAX_VALUE + 1 = Integer.MIN_VALUE
//返回 Integer 值的绝对值。
Math.abs(Integer.MIN_VALUE) =  Integer.MIN_VALUE
Long,short,byte的结论是相同的。
  //这个语句的输出结果:
 System.out.println(1<<31== Integer.MIN_VALUE);//true
文档参考菜鸟教程
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号