Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.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拆分字符串并将该字符串的两部分存储到arraylist中_Java_String_Split - Fatal编程技术网

Java拆分字符串并将该字符串的两部分存储到arraylist中

Java拆分字符串并将该字符串的两部分存储到arraylist中,java,string,split,Java,String,Split,这是一些数据。我想把这些数据分成两部分。第一个是年份(例913),第二个是关于特定年份的信息。然后我想把这些数据作为键值对存储在树映射中。我试着用分裂函数做这件事,但并没有给我满意的结果。所以请引导我完成这项工作。提前谢谢 这是我的密码 String open = "event"; String close = "birth; String text =

这是一些数据。我想把这些数据分成两部分。第一个是年份(例913),第二个是关于特定年份的信息。然后我想把这些数据作为键值对存储在树映射中。我试着用分裂函数做这件事,但并没有给我满意的结果。所以请引导我完成这项工作。提前谢谢

这是我的密码

String open = "event";                                                                       
String close = "birth;
String text = "";
int start = data.indexOf(open);
if (start != -1)
{
     int end = data.indexOf(close, start + open.length());
      if (end != -1)
     {
              text = data.substring(start + open.length(), end);
               text = text.replace("==","");
            String[] words = text.split("–");
               for(String w : words)
                {
                        Log.d("trace w", w.trim());
                }
      }                                                                                                       
}


AD 70 – Emperor Alexander III dies of exhaustion while playing the game tzykanion (Byzantine name for polo). He is succeeded by his 8-year-old nephew Constantine VII.
1513 – Italian Wars: Battle of Novara. Swiss troops defeat the French under Louis II de la Trémoille, forcing the French to abandon Milan. Duke Massimiliano Sforza is restored.
1523 – Gustav Vasa, the Swedish regent, is elected King of Sweden, marking a symbolic end to the Kalmar Union. This is the Swedish national day.
1586 – Francis Drake's forces raid St. Augustine in Spanish Florida.
1644 – The Qing dynasty Manchu forces led by the Shunzhi Emperor capture Beijing during the collapse of the Ming dynasty.
1654 – Queen Christina abdicates the Swedish throne and is succeeded by her cousin Charles X Gustav.
1674 – Shivaji, founder of the Maratha Empire, is crowned.
1749 – The Conspiracy of the Slaves in Malta is discovered.
1762 – Seven Years' War: British forces begin a siege of Havana, Cuba, and temporarily capture the city in the Battle of Havana.
1808 – Napoleon's brother, Joseph Bonaparte, is crowned King of Spain.
1809 – Sweden promulgates a new Constitution, which restores political power to the Riksdag of the Estates after 20 years of enlightened absolutism. At the same time, Charles XIII is elected to succeed Gustav IV Adolf as King of Sweden.
1813 – War of 1812: Battle of Stoney Creek: A British force of 700 under John Vincent defeats an American force twice its size under William Winder and John Chandler.
1822 – Alexis St. Martin is accidentally shot in the stomach, leading to William Beaumont's studies on digestion.
1832 – The June Rebellion in Paris is put down by the National Guard.
1844 – The Young Men's Christian Association (YMCA) is founded in London.
1844 – The Glaciarium, the world's first mechanically frozen ice rink, opens.

如果您的数据在格式上有点恒定,您可以找到“–”的第一个实例。从那里你可以很容易地在每一行中添加子串

像这样:

String s = "AD 70 – Emperor Al... Constantine VII.";
String y = s.substring(0, s.indexOf('–')).trim();
String info = s.substring(s.indexOf('–') + 1).trim();
如果以递归方式执行,则可以将键和值添加到树映射中

不确定这是否是最优雅的解决方案,但它确实有效

A.

ArrayList list=Arrays.asList(您的_String.split(“分隔符”);

以下是实现上述要求的步骤

  • 将字符串拆分为“-”
  • 存储到treemap集合中
  • 下面是一个例子

     public static void convertSplitStringIntoTreeMap() {
        List<String> listOfString = new ArrayList<String>();
        String str = "2012 - This is for testing";
        String str2 = "2013 - This is for testing2";
        listOfString.add(str);
        listOfString.add(str2);
    
        TreeMap<Integer, String> tmap = new TreeMap<Integer, String>();
    
        for (String st : listOfString) {
            String[] splitString = st.split("-");
            tmap.put(Integer.parseInt(splitString[0].trim()), splitString[1]);
        }
    
        System.out.println(tmap);
    }
    
    公共静态void convertSplitStringIntoTreeMap(){
    List listOfString=new ArrayList();
    String str=“2012-这是用于测试”;
    String str2=“2013-这是用于测试2”;
    listOfString.add(str);
    listOfString.add(str2);
    TreeMap tmap=新的TreeMap();
    for(字符串st:listOfString){
    String[]splitString=st.split(“-”);
    tmap.put(Integer.parseInt(splitString[0].trim()),splitString[1]);
    }
    系统输出打印项次(tmap);
    }
    
    java 8替代方案:
    List List=Stream.of(yourString.split(“–”).collect(Collectors.toList())

    我将使用扫描仪从您的数据中进行扫描

        Scanner dataInput = new Scanner(data);
        Map<String, String> dataMap = new HashMap<>();
    
        String input = dataInput.nextLine();
        while (!input.isEmpty()) {
            String[] spliting = input.split("(?=–)", 2);
            dataMap.put(spliting[0], spliting[1]);
            input = dataInput.nextLine();
        } 
    
        dataMap.forEach((k,v) -> System.out.println(k + v));
    
    扫描仪数据输入=新扫描仪(数据);
    Map dataMap=newhashmap();
    String input=dataInput.nextLine();
    而(!input.isEmpty()){
    String[]spliting=input.split((?=–)”,2);
    put(拆分[0],拆分[1]);
    input=dataInput.nextLine();
    } 
    forEach((k,v)->System.out.println(k+v));
    

    该值将保留在–。

    我已经尝试过这样做
    。。。您能给我们看一下您的代码吗?看起来您可以在“-”上拆分“
    是的,我可以在“-”上拆分,但它给出了下面的结果。913年,亚历山大三世皇帝在玩Tzykonion(马球的拜占庭名称)游戏时因精疲力竭而死。他的继任者是他8岁的侄子康斯坦丁七世。1513 1513是第二个字符串的一部分。我想将1513作为数组列表中的新元素。不在“-”上拆分的优点是,如果数据多次包含该字符,则不会得到超过2个字符串。数据不是常量。它可以是多种多样的。但它总是以“东西——更多东西”的形式出现吗?
        Scanner dataInput = new Scanner(data);
        Map<String, String> dataMap = new HashMap<>();
    
        String input = dataInput.nextLine();
        while (!input.isEmpty()) {
            String[] spliting = input.split("(?=–)", 2);
            dataMap.put(spliting[0], spliting[1]);
            input = dataInput.nextLine();
        } 
    
        dataMap.forEach((k,v) -> System.out.println(k + v));