创造独特价值的ArrayList [英] Create an Arraylist of unique values
问题描述
我有,在Java中,这些值的ArrayList(很多行,这只是一个提取物)
20/03/2013 23时31分46秒6870 6810 6800 6720 6860 6670 6700 6650 6750 6830 34864 34272
20/03/2013 23点31分46秒6910 6780 6800 6720 6860 6680 6620 6690 6760 6790 35072 34496
块引用>其中的前两个值是containes数据,并存储在一个单一的元件。弦
我想要做的就是比较字符串数据元素和删除例如提到该行的第二个和所有的元素是什么。
现在,我已经使用了为周期每13元素的字符串相比较(以比较唯一的数据字符串)
我的问题:我可以实现其他更好的解决方案。
?这是我的code:
进口java.util.Scanner中;
进口的java.util.List;
进口的java.util.ArrayList;
进口java.io. *;
进口java.text.SimpleDateFormat的;
进口java.util.Date;公共类下采样{
公共静态无效的主要(字串[] args)抛出异常{ //输入文件
扫描器S =新的扫描仪(新的文件(prova.txt));
//保存输入文件中的每个元素的ArrayList
ArrayList的<串GT;名单=新的ArrayList<串GT;();
而(s.hasNext()){
list.add(s.next());
}
S.CLOSE(); // ArrayList中保存修改后的值
ArrayList的<串GT; DS =新的ArrayList<串GT;(); //
INT I;
对于(i = 0; I< =则为list.size() - 13,I = I + 14){ //第一组合到值,以获得数据
字符串str = list.get(ⅰ)++ list.get第(i + 1);
ds.add(STR);
//添加所有其他值的ArrayList DS
诠释J;
为(J = 2; J&下; 14; J ++){
ds.add(list.get(I + J));
} //比较数据值
时int k;
对于(K = 0; K< = ds.size() - 12; K = K + 13){
ds.get(K); //第一个数据字符串元素
//与其他字符串比较和删除
//去做
}
}
}
}
解决方案
创造独特价值的ArrayList
块引用>您可以使用
Set.toArray()
方法。
这是不包含重复元素的集合。更正式地,集合
不包含对E1和e2元素使得e1.equals(E2)中,在
最多有一个null元素。作为暗示它的名字,这个接口模型
数学集抽象。
块引用><一个href=\"http://docs.oracle.com/javase/6/docs/api/java/util/Set.html\">http://docs.oracle.com/javase/6/docs/api/java/util/Set.html
I have, in Java, an arraylist with those values (many lines, this is just an extract)
20/03/2013 23:31:46 6870 6810 6800 6720 6860 6670 6700 6650 6750 6830 34864 34272 20/03/2013 23:31:46 6910 6780 6800 6720 6860 6680 6620 6690 6760 6790 35072 34496
Where the first two values are strings that containes data and are stored in a single element.
What i want to do is comparing the string data elements and deleting for example the second one and all the elements referred to that line.
For now, i've used a for cycle that every 13 elements compares the string (in order to compare only data strings)
My Question: can i implement other better solutions?
This is my code:
import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; public class Downsampler { public static void main(String[] args) throws Exception{ //The input file Scanner s = new Scanner(new File("prova.txt")); //Saving each element of the input file in an arraylist ArrayList<String> list = new ArrayList<String>(); while (s.hasNext()){ list.add(s.next()); } s.close(); //Arraylist to save modified values ArrayList<String> ds = new ArrayList<String>(); // int i; for(i=0; i<=list.size()-13; i=i+14){ //combining the first to values to obtain data String str = list.get(i)+" "+list.get(i+1); ds.add(str); //add all the other values to arraylist ds int j; for(j=2; j<14; j++){ ds.add(list.get(i+j)); } //comparing data values int k; for(k=0; k<=ds.size()-12; k=k+13){ ds.get(k); //first data string element //Comparing with other strings and delete //TODO } } } }
解决方案Create an Arraylist of unique values
You could use
Set.toArray()
method.A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
http://docs.oracle.com/javase/6/docs/api/java/util/Set.html
这篇关于创造独特价值的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!