【问题标题】:Java formatting a arraylistJava 格式化数组列表
【发布时间】:2017-10-15 19:52:40
【问题描述】:

我得到了一个像这样的 Arraylist

[netl, entl, ltc, 6.3, 6.3, maat, lo, CombiGym, mr, wisB, nat, schk, biol, 6.4, 6.4, wisD, L&W, 9, 8.5, reken, exp]

我希望它看起来像这样

[netl, entl, ltc 6.3 6.3, maat, lo, CombiGym, mr, wisB, nat, schk, biol 6.4 6.4, wisD, L&W 9 8.5, reken, exp]

我不希望数字成为 ArrayList 中的单独对象。 这是我的代码

    ArrayList<String> arrayvakken = new ArrayList<String>();
    String loginFormUrl = "https://example.com/login?path=%2F%3F";
    String loginActionUrl = "https://example.com/login?passAction=login&path=%2F%3F";
    String username = "USERNAME";
    String password = "PASSWORD";

    HashMap<String, String> cookies = new HashMap<>();
    HashMap<String, String> formData = new HashMap<>();

    Connection.Response loginForm = Jsoup.connect(loginFormUrl).method(Connection.Method.GET).userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0").execute();
    Document loginDoc = loginForm.parse(); // this is the document that contains response html

    cookies.putAll(loginForm.cookies()); // save the cookies, this will be passed on to next request


    formData.put("Login", "Inloggen");
    formData.put("wu_loginname", username);
    formData.put("wu_password", password);

    Connection.Response homePage = Jsoup.connect(loginActionUrl)
            .cookies(cookies)
            .data(formData)
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0")
            .method(Connection.Method.POST)
            .execute();

    Document document2 = Jsoup.connect("https://example.com/Portaal/Cijfer_menu/Cijferoverzicht").cookies(cookies).get();
    for(Element element : document2.select(".vak,.wp3-cijfer")) {
        arrayvakken.add(element.select(".vak,.wp3-cijfer").text());
            }
    for(String str : arrayvakken) {
        Pattern p = Pattern.compile("([0-9])");
        Matcher m = p.matcher(str);
        if(m.find()){
            System.out.println("number");
        } else {
            System.out.println("word");
        }
        
    }
        }

我将如何格式化顶部所示的 ArrayList?

【问题讨论】:

  • 该代码与这些数组有什么关系?
  • 列表中的类型是什么??
  • String,我正在使用Jsoup从网站获取数据

标签: java arraylist


【解决方案1】:

我试图猜测您到底想要达到什么目的。假设您有一个包含字符串的 ArrayList,以下代码可能会执行您想要实现的操作(使用方法 updateList 将看起来像您的第一个示例的 ArrayList 更改为看起来像您的第二个示例的 ArrayList):

import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Main
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();
        list.add("netl"); list.add("etnl"); list.add("ltc"); list.add("6.3"); list.add("6.3");
        list.add("maat"); list.add("lo"); list.add("CombiGym"); list.add("mr"); list.add("wisB");
        list.add("nat"); list.add("schk"); list.add("biol"); list.add("6.4"); list.add("6.4");
        list.add("wisD"); list.add("L&W"); list.add("9"); list.add("8.5"); list.add("reken");
        list.add("exp");
        log(list);
        log(updateList(list));
    }

    public static ArrayList<String> updateList(ArrayList<String> list)
    {
        ArrayList<String> ret = new ArrayList<String>();
        for (int i=0; i<list.size(); i++)
        {
            if (i == 0)
                ret.add(list.get(i));
            else
            {
                Pattern p = Pattern.compile("^[0-9]+(\\.[0-9]+)?$");
                Matcher m = p.matcher(list.get(i));
                if (m.find()) ret.set(ret.size()-1, ret.get(ret.size()-1) + " " + list.get(i));
                else ret.add(list.get(i));
            }
        }
        return ret;
    }

    public static void log(ArrayList<String> list)
    {
        String logstr = "[";
        for (int i=0; i<list.size(); i++)
        {
            if (i > 0) logstr += ", ";
            logstr += list.get(i);
        }
        logstr += "]";
        System.out.println(logstr);
    }
}

如果我运行上面的代码,我会得到以下输出:

[netl, etnl, ltc, 6.3, 6.3, maat, lo, CombiGym, mr, wisB, nat, schk, biol, 6.4, 6.4, wisD, L&W, 9, 8.5, reken, exp]
[netl, etnl, ltc 6.3 6.3, maat, lo, CombiGym, mr, wisB, nat, schk, biol 6.4 6.4, wisD, L&W 9 8.5, reken, exp]

【讨论】:

  • 在您的代码中,只需将 for(String str : arrayvakken)-loop 替换为 arrayvakken = updateList(arrayvakken);您显然必须从我的代码示例中复制 updateList 方法才能使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-25
  • 2023-03-12
相关资源
最近更新 更多