Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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_Arrays_String - Fatal编程技术网

有没有办法在java中切掉字符串数组(不使用循环)并直接转换为整数?

有没有办法在java中切掉字符串数组(不使用循环)并直接转换为整数?,java,arrays,string,Java,Arrays,String,这就是我想要实现的目标 输入: 5 1 2 3 -4 -5 -6 -7 -8 -9 预期产出: The list is 1 2 3 -4 -5. There are 3 positive numbers and 2 negative numbers. 示例代码: String[] s = sc.nextLine().split(" "); for(int i=0;i<n;i++) { ... } System.out.printf("The list is %s.\nThere

这就是我想要实现的目标

输入:

5
1 2 3 -4 -5 -6 -7 -8 -9
预期产出:

The list is 1 2 3 -4 -5.
There are 3 positive numbers and 2 negative numbers.
示例代码:

String[] s = sc.nextLine().split(" ");
for(int i=0;i<n;i++)
{
    ...
}
System.out.printf("The list is %s.\nThere are %d positive numbers and %d negative numbers",(s.toString().substring(0,n)),pos,neg);
sc.close();
尝试使用
%d
应用
Integer.parseint(s.toString().substring(0,n))
,但也得到了与上述相同的输出。如何纠正这一点

尝试
Arrays.toString
而不是
s.toString()

更正后的代码如下所示:

String[] s = sc.nextLine().split(" ");
for(int i=0;i<n;i++)
{
    ...
}
System.out.printf("The list is %s.\nThere are %d positive numbers and %d negative numbers",(Arrays.toString(s).substring(0,n)),pos,neg);
sc.close();
String[]s=sc.nextLine().split(“”);
对于(int i=0;i请尝试
Arrays.toString
而不是
s.toString()

更正后的代码如下所示:

String[] s = sc.nextLine().split(" ");
for(int i=0;i<n;i++)
{
    ...
}
System.out.printf("The list is %s.\nThere are %d positive numbers and %d negative numbers",(Arrays.toString(s).substring(0,n)),pos,neg);
sc.close();
String[]s=sc.nextLine().split(“”);

对于(int i=0;i您可以使用
数组。asList
stream
API来获得解决方案。我已经使用
limit
将记录数限制为5,就像您的情况一样。如果您的条件发生变化,您可以使用
filter
并将您的条件放置在那里。下面是一个小代码片段,介绍了如何完成此操作

public static void main(String[] args) {
    String[] s = "1 2 3 -4 -5 -6 -7 -8 -9".split(" ");
    Arrays.asList(s).stream().limit(5).forEach(System.out::print); // filter
   // or use below to get the first five element list
   Arrays.asList(s).stream().limit(5).collect(Collectors.toList());
}

您可以使用
数组。asList
stream
API来获得解决方案。我已经使用
limit
将记录数限制为5,就像您的情况一样。如果您的条件发生变化,您可以使用
filter
并将您的条件放置在那里。下面是一个小代码片段,说明如何完成此操作

public static void main(String[] args) {
    String[] s = "1 2 3 -4 -5 -6 -7 -8 -9".split(" ");
    Arrays.asList(s).stream().limit(5).forEach(System.out::print); // filter
   // or use below to get the first five element list
   Arrays.asList(s).stream().limit(5).collect(Collectors.toList());
}

您可以尝试字符串操作

@Test
public void stringManipulationForArray() {

    int[] intArray = {1,2,3,4,5,6,7,8};
    final String arrayAsStr = Arrays.toString(intArray);
    System.out.println(arrayAsStr );
    final String numbersWithCommas = arrayAsStr .substring(1, s.length() - 1);
    System.out.println(numbersWithCommas);
    final String numbersWithoutCommas = numbersWithCommas.replace(",", "");
    System.out.println(numbersWithoutCommas);
}

您可以尝试字符串操作

@Test
public void stringManipulationForArray() {

    int[] intArray = {1,2,3,4,5,6,7,8};
    final String arrayAsStr = Arrays.toString(intArray);
    System.out.println(arrayAsStr );
    final String numbersWithCommas = arrayAsStr .substring(1, s.length() - 1);
    System.out.println(numbersWithCommas);
    final String numbersWithoutCommas = numbersWithCommas.replace(",", "");
    System.out.println(numbersWithoutCommas);
}

s.toString()
更改为
数组。toString
侧注:使用
printf
@Eran时,您可能希望使用
%n
而不是
\n
,并且它仍然给出
作为输出的列表是[1,2.
。将
s.toString()
更改为
数组。toString(s)
旁注:当使用
printf
@时,您可能想使用
%n
而不是
\n
,并且它仍然给出
列表是[1,2.
作为输出。它仍然给出列表是[1,2.作为输出。这是因为您得到的子字符串是从0到n(
子字符串(0,n)
)。
数组
应该给你这个字符串:
[1,2,3,-4,-5,-6,-7,-8,-9]
。现在你可以使用子字符串来切掉你不想要的字符串部分,你应该得到
1,2,3,-4,-5
。我认为
子字符串(1,n*3+neg-1)
应该适合你。它仍然给出了列表[1,2.作为输出。这是因为子字符串从0到n(
substring(0,n)
)。
数组。toString(s)
应该为您提供以下字符串:
[1,2,3,-4,-5,-6,-7,-8,-9]
。现在您可以使用子字符串来切断字符串中不需要的部分,您应该得到
1,2,3,-4,-5
。我认为
子字符串(1,n*3+neg-1)
应该适合您。希望解决方案在打印时没有循环。@Ekrem更新了我的答案希望解决方案在打印时没有循环。@Ekrem更新了我的答案