微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

需要弄清楚如何将数组列表中的逗号替换为空格

如何解决需要弄清楚如何将数组列表中的逗号替换为空格

我已尝试使用 replaceAllremoveAll 但仍然无法更改没有逗号的列表。 我试图在 list + "") 之后添加;但什么都没有改变。仍然使用逗号获取输出。 我需要删除输出中的逗号。数字之间只需要一个空格。 (见最后输出

删除重复项)编写一个方法,使用以下标头从整数数组列表中删除重复项:
public static void removeDuplicate(ArrayList<Integer> list)
编写一个测试程序,提示用户在列表中输入 10 个整数,并显示一个空格分隔的不同整数。

package exercise11_13;

import java.util.ArrayList;
import java.util.Scanner;

public class Exercise11_13 {

    /**
     * @param args the command line arguments
     */
    // Main method
    public static void main(String[] args) {
        // Create de Scanner object.
        Scanner input = new Scanner(system.in);
        
        // Prompt the user to enter ten integer numbers. 
        System.out.print("Please,enter ten (10) integers numbers: ");
        
        // Create the ListArray (List object).
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) list.add(input.nextInt());

        removeDuplicate(list);
        
        System.out.println("display the distinct integers in their input order "
                + "and seperated by exactly one space: " + "\n" + list );
    }

    public static void removeDuplicate(ArrayList<Integer> list) {

        ArrayList<Integer> temp = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {

            if (!temp.contains(list.get(i))) {
                temp.add(list.get(i));
            }
        }
        list.clear();
        list.addAll(temp);
        
    }
}

输出:我需要删除输出中的逗号。数字之间只需要一个空格。 运行:

Please,enter ten (10) integers numbers: 78
99
54
63
56
78
63
14
67
99
display the distinct integers in their input order and seperated by exactly one space: 
[78,99,54,63,56,14,67]
BUILD SUCCESSFUL (total time: 27 seconds)

解决方法

当您使用这样的对象时,它会通过其 toString 方法隐式转换为字符串。您不应该依赖列表的 toString 实现,而是将其转换为您自己需要的字符串。在这里,您可以流式传输列表并加入一个空格:

System.out.println(
   "Display the distinct integers in their input order and separated by exactly one space: \n" + 
   list.stream().map(Object::toString).collect(Collectors.joining(" "))
);
,

由于问题标有 replaceAll,因此确实可以使用正则表达式 List::toString(括号应该用 [,\[\]]) 转义:

\

或者,知道括号是第一个和最后一个字符,可以在List<Integer> list = Arrays.asList(1,2,3,4,5); System.out.println(list.toString().replaceAll("[,\\[\\]]","")); // 1 2 3 4 5 的帮助下去掉,然后应用substring

String::replace
,

在这个程序中有两种方法可以替换空格的逗号。

长篇: 需要补充: 导入 java.util.stream.Collectors;

System.out.println("Display the distinct integers in their input "
                + "order and separated by exactly one space: "
                + "\n" 
                + 
                list.stream().map(Object::toString).collect(Collectors.joining("")));

简短的:

System.out.println("\nDisplay the distinct integers in their input "
                + "order and separated by exactly one space: "
                + "\n" + list.toString().replaceAll("[,""));  

在整个程序下方。

package exercise11_13;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.stream.Collectors;

    public class Exercise11_13 {
    
        /**
         * @param args the command line arguments
         */
        // Main method
        public static void main(String[] args) {
            // Create de Scanner object.
            Scanner input = new Scanner(System.in);
            
            // Prompt the user to enter ten integer numbers. 
            System.out.print("Please,enter ten (10) integers numbers: ");
            
            // Create the ListArray (List object).
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) list.add(input.nextInt());
    
            removeDuplicate(list);
            /**
            System.out.println("Display the distinct integers in their input "
                    + "order and separated by exactly one space: "
                    + "\n" + 
            list.stream().map(Object::toString).collect(Collectors.joining(" ")));
            */
            System.out.println("\nDisplay the distinct integers in their input "
                    + "order and separated by exactly one space: "
                    + "\n" + list.toString().replaceAll("[,""));         
        }
    
        public static void removeDuplicate(ArrayList<Integer> list) {
    
            ArrayList<Integer> temp = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
    
                if (!temp.contains(list.get(i))) {
                    temp.add(list.get(i));
                }
            }
            list.clear();
            list.addAll(temp);
            
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?