在 ArrayList 上排序,但 ArrayList 具有相同的值

Posted

技术标签:

【中文标题】在 ArrayList 上排序,但 ArrayList 具有相同的值【英文标题】:sorting on ArrayList but ArrayList have same value 【发布时间】:2013-07-16 00:34:01 【问题描述】:

我的文字是

---- SınİSırU, SınİUzO puanları azalan :
------ deneme deneme denemeANYALTD. ŞTİ.1.751.2514.0010.001.751.2false
------ deneme deneme deneme DZLİ ŞTİ.1.431.1411.439.141.430.57false
------ deneme deneme deneme MEİN ŞTİ.1.291.1010.298.761.290.55false
------ deneme deneme deneme HAY ŞTİ.1.291.1010.298.761.290.55false
------ deneme deneme deneme AHAN ŞTİ.1.291.1010.298.761.290.55false
------ deneme deneme deneme MSA ŞTİ.1.291.1010.298.761.290.55false
------ deneme deneme deneme Ş ŞTİ.1.291.1010.298.761.290.55false
------ deneme deneme deneme KO. ŞTİ.1.251.0810.008.671.250.54false

kolon1:1.75,1.43,1.29...

kolon2:1.25,1.14,1.10..

然后我将它们从小到大排序。

有替换号码我不想替换号码。例如,如果我包含的文本包含 3,3,3,3,3,2,2,2,2,1,1,1,1,1,1

我只想要 1 2 3

 public class PointStatue 


    private static List<Double> col1 = new ArrayList<Double>(); 
    private static List<Double> col2 = new ArrayList<Double>();
    private static List<Double> col3 = new ArrayList<Double>();
    private static  List<Double> col4 = new ArrayList<Double>();
    private static List<Double> col5 = new ArrayList<Double>();
    private static List<Double> col6 = new ArrayList<Double>();
    private static List<Double> list=new ArrayList<Double>();


    private static List<Double> numbers = new ArrayList<Double>();

    public static void main(String[] args) throws IOException 

        ArrayList<String> puan;
        puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
        System.out.format("%d kayıt okundu.%n", puan.size());

        for (int j = 0; j < puan.size(); j++) 

            String point = puan.get(j);

            String[] edit = point.split("[\\\\\\)]");

            for (String s : edit) 

                String a = s.replaceAll("[\\\\-\\>\\>\\]\\[\\#\\*\\]+", "");
                String b = a.replaceAll("[\\.]+", ",");
                Scanner scanner = new Scanner(b);

                while (scanner.hasNext()) 

                    if (scanner.hasNextDouble()) 
                        Double doubleValue = scanner.nextDouble();  
                        numbers.add(doubleValue);
                        // System.out.println(Arrays.deepToString(numbers.toArray()));

                    // if scan end

                    else 

                        //if it is string comes value here
                        String stringValue = scanner.next();

                    // if scan end

                // while end

            // for string s

        // for j end


        int col=1;

        for(int i=0;i<numbers.size();i++)
        

            Double rowValue = numbers.get(i);

            switch(col)
            
            case 1:
                col1.add(rowValue);
                Collections.sort(col1);
                col++;
                break;
            case 2:
                col2.add(rowValue);
                Collections.sort(col2);
                col++;
                break;
            case 3:
                col3.add(rowValue);
                Collections.sort(col3);
                col++;
                break;
            case 4:
                col4.add(rowValue);
                Collections.sort(col4);
                col++;
                break;
            case 5:
                col5.add(rowValue);
                Collections.sort(col5);
                col++;
                break;
            case 6:
                col6.add(rowValue);
                Collections.sort(col6);
                col = 1;
                break;

            //switch end

        //for i end


        System.out.println("kolon1"+col1);
        System.out.println("kolon2"+col2);
        System.out.println("kolon3"+col3);
        System.out.println("kolon4"+col4);
        System.out.println("kolon5"+col5);
        System.out.println("kolon6"+col6);

    // main end

    private static ArrayList<String> okuDiziyeKoy(String dosyaAdı) 

        ArrayList<String> dizi = new ArrayList<String>();
        try 
            FileInputStream fIS;
            fIS = new FileInputStream(dosyaAdı);
            Reader r = new InputStreamReader(fIS, "ISO-8859-9");
            BufferedReader bR = new BufferedReader(r);
            String satır;

            while ((satır = bR.readLine()) != null) 
                dizi.add(satır);

            
         catch (FileNotFoundException e) 
            e.printStackTrace();
         catch (UnsupportedEncodingException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        
        return dizi;
    // okuDiziyeKoyEnd

// class end

【问题讨论】:

不能用 Set 代替 List 吗?例如看看 HashSet 我不想改变我的arraylist值..我只想要块替换数字 【参考方案1】:

看看TreeSet

基于 TreeMap 的 NavigableSet 实现。元素是 使用它们的自然顺序排序,或由提供的比较器排序 设置创建时间,取决于使用的构造函数。

这应该确保您只有一个元素,并且列表根据您的需要进行排序。

【讨论】:

但我不应该改变我的真实价值,因为之后这段代码将是 1500 双值 @user2583040,它不会改变实际值 - TreeSet 同时排序和重复数据删除,这正是您所要求的。如果您希望在完成后将值返回到 ArrayList 中,只需调用 new ArrayList(treeSetInstance); @user2583040:正如@David 建议的那样,您需要做的就是遍历当前列表并将您的值放入TreeSet。该集合将自动对数据中可能存在的任何重复项进行排序和删除。 @npinti 你有什么例子吗?因为我从来没有听说过treeset :( @user2583040:您可以在这里查看:tutorialspoint.com/java/java_treeset_class.htm 这是一个非常基本的示例,但它应该涵盖您需要的内容。您也可以提供一个比较器,以便您可以选择对象的排序方式。我建议您查看 Javadoc(链接在我的答案中)以获取更多信息。【参考方案2】:

简单的朋友:)

package com.blogspot.arashmd.examples;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DupRemove 
public static void main(String...arg) 
  double val[]=1,4.3,5,2,4,4,4,6.5,2,2,2,2,2,3.5,3.7,4,7,8,9;
  for(int i=0;i<val.length;i++)
    add(val[i]);
  
  Collections.sort(list);
  System.out.println(list);
 
static List<Double> list=new ArrayList<>();
public static void add(double d)
  if(!list.contains(d))list.add(d);


和你的代码业务

/*
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DupRemove 
public static void main(String...arg) 
  double val[]=1,4.3,5,2,4,4,4,6.5,2,2,2,2,2,3.5,3.7,4,7,8,9;
  for(int i=0;i<val.length;i++)
    add(val[i]);
  
  Collections.sort(list);
  System.out.println(list);
 
static List<Double> list=new ArrayList<>();
public static void add(double d)
  if(!list.contains(d))list.add(d);


*/

    import java.io.IOException;
    import java.util.*;
    public class PointStatue 
    private static List<Double> col1 = new ArrayList<Double>(); 
    private static List<Double> col2 = new ArrayList<Double>();
    private static List<Double> col3 = new ArrayList<Double>();
    private static  List<Double> col4 = new ArrayList<Double>();
    private static List<Double> col5 = new ArrayList<Double>();
    private static List<Double> col6 = new ArrayList<Double>();
    private static List<Double> list=new ArrayList<Double>();


    private static List<Double> numbers = new ArrayList<Double>();

    public static void main(String[] args) throws IOException 

        ArrayList<String> puan;
        puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
        System.out.format("%d kayıt okundu.%n", puan.size());
        Double doubleValue;
        for (int j = 0; j < puan.size(); j++) 

            String point = puan.get(j);

            String[] edit = point.split("[\\\\\\)]");

            for (String s : edit) 

                String a = s.replaceAll("[\\\\-\\>\\>\\]\\[\\#\\*\\]+", "");
                String b = a.replaceAll("[\\.]+", ",");
                Scanner scanner = new Scanner(b);

                while (scanner.hasNext()) 

                    if (scanner.hasNextDouble()) 

                        doubleValue = scanner.nextDouble();
                        if(!numbers.contains(doubleValue))
                        numbers.add(doubleValue);
                        
                        // System.out.println(Arrays.deepToString(numbers.toArray()));

                    // if scan end

                    else 

                        //if it is string comes value here
                        String stringValue = scanner.next();

                    // if scan end

                // while end

            // for string s

        // for j end


        int col=1;

        for(int i=0;i<numbers.size();i++)
        

            Double rowValue = numbers.get(i);

            switch(col)
            
            case 1:
                col1.add(rowValue);

                col++;
                break;
            case 2:
                col2.add(rowValue);

                col++;
                break;
            case 3:
                col3.add(rowValue);

                col++;
                break;
            case 4:
                col4.add(rowValue);

                col++;
                break;
            case 5:
                col5.add(rowValue);

                col++;
                break;
            case 6:
                col6.add(rowValue);

                col = 1;
                break;

            //switch end

        //for i end
        Collections.sort(col1);
        Collections.sort(col2);
        Collections.sort(col3);
        Collections.sort(col4);
        Collections.sort(col5);
        Collections.sort(col6);
        System.out.println("kolon1"+col1);
        System.out.println("kolon2"+col2);
        System.out.println("kolon3"+col3);
        System.out.println("kolon4"+col4);
        System.out.println("kolon5"+col5);
        System.out.println("kolon6"+col6);

    // main end  

【讨论】:

【参考方案3】:

试试这个,它会在所有行中找到重复值,而且每一行也对我有用:)

/*
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DupRemove 
public static void main(String...arg) 
  double val[]=1,4.3,5,2,4,4,4,6.5,2,2,2,2,2,3.5,3.7,4,7,8,9;
  for(int i=0;i<val.length;i++)
    add(val[i]);
  
  Collections.sort(list);
  System.out.println(list);
 
static List<Double> list=new ArrayList<>();
public static void add(double d)
  if(!list.contains(d))list.add(d);


*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class PointStatue 
/*private static List<Double> col1 = new ArrayList<Double>(); 
private static List<Double> col2 = new ArrayList<Double>();
private static List<Double> col3 = new ArrayList<Double>();
private static List<Double> col4 = new ArrayList<Double>();
private static List<Double> col5 = new ArrayList<Double>();
private static List<Double> col6 = new ArrayList<Double>();
private static List<Double> col7 = new ArrayList<Double>();
private static List<Double> col8 = new ArrayList<Double>();*/
//private static List<Double> list=new ArrayList<Double>();
private static ArrayList<List<Double>> rows=new ArrayList<List<Double>>();

private static List<Double> numbers = new ArrayList<Double>();
private static void okuDiziyeKoy(String path,ArrayList<String> arr) throws Exception
  BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
  //skip the first line
  br.readLine();
  String g;
  while((g=br.readLine())!=null)
    arr.add(g);
  

public static void main(String[] args) throws Exception 
    ArrayList<String> puan = new ArrayList<String>();
    okuDiziyeKoy("C:/deneme/HW.txt", puan);
    //   puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
    System.out.format("%d kayıt okundu.%n", puan.size());
    Double doubleValue;

    for (int j = 0; j < puan.size(); j++) 
      List<Double> listToAdd=new ArrayList<Double>();
        rows.add(j, listToAdd);
        String point = puan.get(j);
        //split data(line) by 
        String[] edit = point.split("[][]");
        for(int l=3;l<edit.length-1;l++)
          //parsing String as double
          doubleValue=Double.parseDouble(edit[l]);
          //check if double value is not exist then add it to list
          if(!numbers.contains(doubleValue))
          numbers.add(doubleValue);
          
          if(!listToAdd.contains(doubleValue))
            listToAdd.add(doubleValue);
          
        
       /* for (String s : edit) 

            String a = s.replaceAll("[\\\\-\\>\\>\\]\\[\\#\\*\\]+", "");
            String b = a.replaceAll("[\\.]+", ",");
            Scanner scanner = new Scanner(b);

            while (scanner.hasNext()) 

                if (scanner.hasNextDouble()) 

                    doubleValue = scanner.nextDouble();
                    if(!numbers.contains(doubleValue))
                    numbers.add(doubleValue);
                    
                    // System.out.println(Arrays.deepToString(numbers.toArray()));

                // if scan end

                else 

                    //if it is string comes value here
                    String stringValue = scanner.next();

                // if scan end

            // while end

        // for string s

    // for j end*/

        
    Collections.sort(numbers);
    System.out.println("Unique values(in all rows): ");
    for(Double d :numbers)
        System.out.print(d+" ");
      
    System.out.println();
    /*int col=1;
    for(int i=0;i<numbers.size();i++)
    

        Double rowValue = numbers.get(i);

        switch(col)
        
        case 1:
            col1.add(rowValue);

            col++;
            break;
        case 2:
            col2.add(rowValue);

            col++;
            break;
        case 3:
            col3.add(rowValue);

            col++;
            break;
        case 4:
            col4.add(rowValue);

            col++;
            break;
        case 5:
            col5.add(rowValue);

            col++;
            break;
        case 6:
            col6.add(rowValue);

            col = 1;
            break;

        //switch end

    //for i end*/
   /* Collections.sort(col1);
    Collections.sort(col2);
    Collections.sort(col3);
    Collections.sort(col4);
    Collections.sort(col5);
    Collections.sort(col6);*/
    /*System.out.println("kolon1 "+col1);
    System.out.println("kolon2 "+col2);
    System.out.println("kolon3 "+col3);
    System.out.println("kolon4 "+col4);
    System.out.println("kolon5 "+col5);
    System.out.println("kolon6 "+col6);*/
    //sorting each row
    for(int i=0;i<rows.size();i++)
      Collections.sort(rows.get(i));
    
    //showing each row(array)
    for(int i=0;i<rows.size();i++)
      System.out.println("kolon"+i+": "+rows.get(i));
    

// main end  

在列中重复:

/*
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DupRemove 
public static void main(String...arg) 
  double val[]=1,4.3,5,2,4,4,4,6.5,2,2,2,2,2,3.5,3.7,4,7,8,9;
  for(int i=0;i<val.length;i++)
    add(val[i]);
  
  Collections.sort(list);
  System.out.println(list);
 
static List<Double> list=new ArrayList<>();
public static void add(double d)
  if(!list.contains(d))list.add(d);


*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class PointStatue 
/*private static List<Double> col1 = new ArrayList<Double>(); 
private static List<Double> col2 = new ArrayList<Double>();
private static List<Double> col3 = new ArrayList<Double>();
private static List<Double> col4 = new ArrayList<Double>();
private static List<Double> col5 = new ArrayList<Double>();
private static List<Double> col6 = new ArrayList<Double>();
private static List<Double> col7 = new ArrayList<Double>();
private static List<Double> col8 = new ArrayList<Double>();*/
//private static List<Double> list=new ArrayList<Double>();
private static ArrayList<List<Double>> rows=new ArrayList<List<Double>>();

private static List<Double> numbers = new ArrayList<Double>();
private static void okuDiziyeKoy(String path,ArrayList<String> arr) throws Exception
  BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
  //skip the first line
  br.readLine();
  String g;
  while((g=br.readLine())!=null)
    arr.add(g);
  

public static void main(String[] args) throws Exception 
    ArrayList<String> puan = new ArrayList<String>();
    okuDiziyeKoy("C:/deneme/HW.txt", puan);
    //   puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
    System.out.format("%d kayıt okundu.%n", puan.size());
    Double doubleValue;
    for (int j = 0; j < 6; j++) 
      rows.add(new ArrayList<Double>());
    
    for (int j = 0; j < puan.size(); j++) 
        String point = puan.get(j);
        //split data(line) by 
        String[] edit = point.split("[][]");
        for(int l=3;l<edit.length-1;l++)
          //parsing String as double
          doubleValue=Double.parseDouble(edit[l]);
          //check if double value is not exist then add it to list
          if(!numbers.contains(doubleValue))
          numbers.add(doubleValue);
          
          if(!rows.get(l-3).contains(doubleValue))
            rows.get(l-3).add(doubleValue);
          
        
       /* for (String s : edit) 

            String a = s.replaceAll("[\\\\-\\>\\>\\]\\[\\#\\*\\]+", "");
            String b = a.replaceAll("[\\.]+", ",");
            Scanner scanner = new Scanner(b);

            while (scanner.hasNext()) 

                if (scanner.hasNextDouble()) 

                    doubleValue = scanner.nextDouble();
                    if(!numbers.contains(doubleValue))
                    numbers.add(doubleValue);
                    
                    // System.out.println(Arrays.deepToString(numbers.toArray()));

                // if scan end

                else 

                    //if it is string comes value here
                    String stringValue = scanner.next();

                // if scan end

            // while end

        // for string s

    // for j end*/

        
    Collections.sort(numbers);
    System.out.println("Unique values(in all rows): ");
    for(Double d :numbers)
        System.out.print(d+" ");
      
    System.out.println();
    /*int col=1;
    for(int i=0;i<numbers.size();i++)
    

        Double rowValue = numbers.get(i);

        switch(col)
        
        case 1:
            col1.add(rowValue);

            col++;
            break;
        case 2:
            col2.add(rowValue);

            col++;
            break;
        case 3:
            col3.add(rowValue);

            col++;
            break;
        case 4:
            col4.add(rowValue);

            col++;
            break;
        case 5:
            col5.add(rowValue);

            col++;
            break;
        case 6:
            col6.add(rowValue);

            col = 1;
            break;

        //switch end

    //for i end*/
   /* Collections.sort(col1);
    Collections.sort(col2);
    Collections.sort(col3);
    Collections.sort(col4);
    Collections.sort(col5);
    Collections.sort(col6);*/
    /*System.out.println("kolon1 "+col1);
    System.out.println("kolon2 "+col2);
    System.out.println("kolon3 "+col3);
    System.out.println("kolon4 "+col4);
    System.out.println("kolon5 "+col5);
    System.out.println("kolon6 "+col6);*/
    //sorting each row
    for(int i=0;i<rows.size();i++)
      Collections.sort(rows.get(i));
    
    //showing each row(array)
    for(int i=0;i<rows.size();i++)
      System.out.println("kolon"+(i+1)+": "+rows.get(i));
    

// main end  

【讨论】:

我的快乐伙伴,你得到答案了吗?效果好吗?我希望没有任何问题/问题?哈利路亚 :) dude =) 你放了 n 个可隆,但如果你看我的文字,有 6 个数字。我取第一个数字,如 1.75、1.43、1.29 1.29。我把它们放在一个数组上,2。数字到另一个数组,我有 6 个数组,因为我在所有句子附近有 6 个点。然后我对它们进行排序.. 现在你拿 n kolons。但我必须拿 6 克朗,因为我只拿 6 分。我尝试应用我的代码,但我不能。对不起,伙计:(我慢慢来..非常非常感谢.. 这段代码完全按照你的意愿工作,它包含每行一个列表,你的文本文件包含 8 行,每行有 6 个点,我还将所有数字添加到 numbers 列表中, 并且rows 包含每个行值的列表,在 for 循环中它检查重复然后插入,但是对于列也很容易,我也会为列添加它 :) 我更新了答案,检查第二个,一个,可能你需要这个,我希望:)) 它的工作非常好 :D 你是我心目中世界上最好的编码器.. 非常感谢你 =)))) 非常感谢...

以上是关于在 ArrayList 上排序,但 ArrayList 具有相同的值的主要内容,如果未能解决你的问题,请参考以下文章

ArrayList三种遍历方法

2 ArrayList 详解

ArrayList的使用

arrayList和linkedList在排序上的区别

Java之集合ArrayList

java中ArrayList和LinkedList区别