Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何筛选整数并将其从字符串复制到新数组_Java_Arraylist_Casting_Integer_Chars - Fatal编程技术网

Java 如何筛选整数并将其从字符串复制到新数组

Java 如何筛选整数并将其从字符串复制到新数组,java,arraylist,casting,integer,chars,Java,Arraylist,Casting,Integer,Chars,有一个字符串(任何字符串)。它可能不包含数字 如何将该数组中包含的整数存储到ArrayList中 试图 public ArrayList<Integer> toList() { String stringToList; ArrayList<Integer> l = new ArrayList<>(); stringToList = this.toString(); // I am just copying the string from

有一个字符串(任何字符串)。它可能不包含数字

如何将该数组中包含的整数存储到ArrayList中

试图

public ArrayList<Integer> toList() {
    String stringToList;
    ArrayList<Integer> l = new ArrayList<>();
    stringToList = this.toString(); // I am just copying the string from the object
    for (int i = 0; i < stringToList.length(); i++) {
        if(((Integer) stringToList.charAt(i)) instanceof Integer){
                 //Here store it into the arrayList
                }             
    }
    return l;
}
public ArrayList toList(){
弦乐演奏家;
ArrayList l=新的ArrayList();
stringToList=this.toString();//我只是从对象复制字符串
对于(int i=0;i

但很明显,这是行不通的,因为演员阵容不是解决方案。有什么想法吗?谢谢。

由于字符串中可能存在如
12
,因此不能直接使用
charAt()。您可以使用
拆分
stringToList
,然后使用
Integer.parseInt()

public ArrayList toList(){
弦乐演奏家;
ArrayList l=新的ArrayList();
stringToList=this.toString();//我只是从对象复制字符串
String[]strings=stringToList.split(“,\\s+”);
for(int i=0;i
由于字符串中可能存在如
12
,因此不能直接使用
charAt()。您可以使用
拆分
stringToList
,然后使用
Integer.parseInt()

public ArrayList toList(){
弦乐演奏家;
ArrayList l=新的ArrayList();
stringToList=this.toString();//我只是从对象复制字符串
String[]strings=stringToList.split(“,\\s+”);
for(int i=0;i
您可以拆分字符串,如

String[] splits = stringToList.split(",");

并使用类似于
integer.parseInt(splits[0])的东西将它们转换为整数并将其存储在ArrayList中

您可以拆分字符串,如

String[] splits = stringToList.split(",");

并使用类似于
integer.parseInt(splits[0])的东西将它们转换为整数并将其存储在ArrayList中

您可以使用character.isDigit方法检查字符是否为数字

if (Character.isDigit(stringToList.charAt(0))){
    l.add((int)stringToList.charAt(0));
}

并将字符强制转换为int,然后再将其添加到整数数组列表中

您可以使用character.isDigit方法检查字符是否为数字

if (Character.isDigit(stringToList.charAt(0))){
    l.add((int)stringToList.charAt(0));
}
并将字符强制转换为int,然后再将其添加到整数数组列表中

以单行方式解析、检查、转换、收集:

此解决方案可以解析任何
字符串
,因为它将在转换为
整数
之前检查从拆分中提取的每个部分是否仅由数字组成(或之前带有负号)

String str=“1,2,2a,3,-14,b55”;
List=Arrays.stream(str.split(“,”))
.map(字符串::trim)
.filter(s->s.matches(“-?\\d+”)
.map(整数::valueOf)
.collect(Collectors.toList());
System.out.println(列表);//名单:[1,2,3,-14]
  • 在逗号上拆分
    字符串
  • 制作一个
  • 删除周围的
    空格
  • 检查是否只有数字
  • 将每个元素转换为
    整数
  • 将它们收集到
    列表中
    • 以单行方式解析、检查、转换、收集:

      此解决方案可以解析任何
      字符串
      ,因为它将在转换为
      整数
      之前检查从拆分中提取的每个部分是否仅由数字组成(或之前带有负号)

      String str=“1,2,2a,3,-14,b55”;
      List=Arrays.stream(str.split(“,”))
      .map(字符串::trim)
      .filter(s->s.matches(“-?\\d+”)
      .map(整数::valueOf)
      .collect(Collectors.toList());
      System.out.println(列表);//名单:[1,2,3,-14]
      
      • 在逗号上拆分
        字符串
      • 制作一个
      • 删除周围的
        空格
      • 检查是否只有数字
      • 将每个元素转换为
        整数
      • 将它们收集到
        列表中

      如果您使用的是Java 8,您可以尝试以下方法:

      public ArrayList<Integer> toList(String input) {
          return Arrays.stream(input.split(","))
                       .map(String::trim)
                       .filter(s -> s.matches("\\b\\d+\\b"))
                       .map(Integer::valueOf)
                       .collect(Collectors.toCollection(ArrayList::new));
      
      }
      
      public ArrayList toList(字符串输入){
      返回Arrays.stream(input.split(“,”))
      .map(字符串::trim)
      .filter(s->s.matches(“\\b\\d+\\b”))
      .map(整数::valueOf)
      .collect(收集器.toCollection(ArrayList::new));
      }
      
      如果您使用的是Java 8,可以尝试以下操作:

      public ArrayList<Integer> toList(String input) {
          return Arrays.stream(input.split(","))
                       .map(String::trim)
                       .filter(s -> s.matches("\\b\\d+\\b"))
                       .map(Integer::valueOf)
                       .collect(Collectors.toCollection(ArrayList::new));
      
      }
      
      public ArrayList toList(字符串输入){
      返回Arrays.stream(input.split(“,”))
      .map(字符串::trim)
      .filter(s->s.matches(“\\b\\d+\\b”))
      .map(整数::valueOf)
      .collect(收集器.toCollection(ArrayList::new));
      }
      
      代码示例:

      说明:[^-?0-9]+

      +在一次和无限次之间,尽可能多次,根据需要给予回报

      -??其中一个字符是“-?”


      0-9介于“0”和“9”之间的字符

      代码示例:

      说明:[^-?0-9]+

      +在一次和无限次之间,尽可能多次,根据需要给予回报

      -??其中一个字符是“-?”

      0-9介于“0”和“9”之间的字符

      必须先将当前字符“转换”为整数对象,然后将其选中为空。您的代码可能会遇到错误