2017-10-15 101 views
-6

我看起来像这样的的Java格式化的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。 这是我的代码

ArrayList<String> arrayvakken = new ArrayList<String>(); 
    String loginFormUrl = "https://werkers.wpkeesboeke.nl/login?path=%2F%3F"; 
    String loginActionUrl = "https://werkers.wpkeesboeke.nl/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://werkers.wpkeesboeke.nl/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,如上图所示?

+2

是什么代码都和这两个阵列的呢? –

+0

列表中的类型是什么? –

+0

字符串,我使用Jsoup从网站获取数据 – Ropro

回答

0

我试图猜测你到底想要达到什么目的。假设你有一个包含字符串的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] 
+0

在你的代码中,只需用arrayvakken = updateList(arrayvakken)替换for(String str:arrayvakken)-loop;您无疑需要从我的代码示例中复制updateList方法才能使用它。 –