算法用到中的函数

数学指数运算

//a是底数  b是指数
Math.pow(a,b);


//数组求和
int[] jobs;
Arrays.stream(jobs).sum();

将List<Integer>转为int[]数组

int[] array = list.stream().mapToInt(i->i).toArray();

对数组进行排序

Arrays.sort(arr);

对链表进行排序

Collections.sort(list);

Java Character.isDigit() 方法,判断字符是否为数字

语法

boolean isDigit(char ch)

参数

  • ch -- 要测试的字符。

返回值

如果字符为数字,则返回 true;否则返回 false。

实例

public class Test {

	public static void main(String args[]) {
		System.out.println(Character.isDigit('c'));
		System.out.println(Character.isDigit('5'));
	}
}

以上程序执行结果为:

false true

如何把数字字符转换为整数

  1. Integer.parseInt(char);
  2. s.charAt(index) - '0'

Java数组Arrays.fill()方法

boolean[] a1 = new boolean[5];
Arrays.fill( a1,true );
结果 a1[] = {true,true,true,true,true};

 

String[] a = new String[6];
Arrays.fill(a, "Hello");
Arrays.fill(a, 3, 5,"World"); //3是开始下标  5是结束下标
结果是 a[] = {Hello,Hello,Hello,World,World,Hello};

String字符串转换为字符数组

String s = "*******";

char[] ch = s.toCharArray();

把字符串数组转换为字符

       char[] ch = new char[10];

       String.valueOf(ch);

使用Arrays.sort()对数组进行排序

int indexOf(String str)
 返回指定子字符串在此字符串中第一次出现处的索引。

String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。

比如删掉字符串中所有空格  Str.replaceAll(" ", "");

String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。

String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。

contains(CharSequence chars)
判断是否包含指定的字符系列。

StringBuilder的append的方法

当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。

在使用 StringBuffer 类时,每次都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,所以如果需要对字符串进行修改推荐使用 StringBuffer。

StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。

由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。

实例

public class RunoobTest{
    public static void main(String args[]){
        StringBuilder sb = new StringBuilder(10);
        sb.append("Runoob..");
        System.out.println(sb);  
        sb.append("!");
        System.out.println(sb); 
        sb.insert(8, "Java");
        System.out.println(sb); 
        sb.delete(5,8);
        System.out.println(sb);  
    }
}

 replace(int start, int end, String str)
使用给定 String 中的字符替换此序列的子字符串中的字符。

清空List

1.用list.clear()方法清空list;用此方法,其它引用该list的值也会变成空。

list2 = list;//此时list2有值
list.clear();//先将list赋值给list2,再清空,此时list和list2都为空

2.用list = null来清空list

3.new ArrayList()来清空list

数组转List

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
List staffsList = Arrays.asList(staffs);

需要注意的是, Arrays.asList() 返回一个受指定数组决定的固定大小的列表。所以不能做 add 、 remove 等操作,否则会报错。

List staffsList = Arrays.asList(staffs);
staffsList.add("Mary"); // UnsupportedOperationException
staffsList.remove(0); // UnsupportedOperationException

如果想再做增删操作呢?将数组中的元素一个一个添加到列表,这样列表的长度就不固定了,可以进行增删操作

List staffsList = new ArrayList<String>();
for(String temp: staffs){
  staffsList.add(temp);
}
staffsList.add("Mary"); // ok
staffsList.remove(0); // ok

数组转Set

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
Set<String> staffsSet = new HashSet<>(Arrays.asList(staffs));
staffsSet.add("Mary"); // ok
staffsSet.remove("Tom"); // ok

List转数组

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
List staffsList = Arrays.asList(staffs);

Object[] result = staffsList.toArray();

List转Set

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
List staffsList = Arrays.asList(staffs);

Set result = new HashSet(staffsList);

Set转数组

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
Set<String> staffsSet = new HashSet<>(Arrays.asList(staffs));

Object[] result = staffsSet.toArray();

Set转List

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
Set<String> staffsSet = new HashSet<>(Arrays.asList(staffs));

List<String> result = new ArrayList<>(staffsSet);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值