特定の時点で保存された時間を含む 4 つのコレクションがあります。私の質問はかなり一般的なものなので、コレクションに含まれるものは重要ではありません。これらのコレクションを同時にループし、サイズを取得して最長のコレクションを返す方法を考えていました。これは理にかなっていますか?
4 に答える
3
コレクションのサイズを知るためにコレクションを反復処理する必要があるのはなぜですか? コレクションにはsize()
メソッドがあります
于 2013-06-13T08:56:44.610 に答える
0
ここに例があります -
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
List<Integer> l4=new ArrayList<Integer>();
l1.add(1);l1.add(1);
l2.add(2);l2.add(2);l2.add(2);
l3.add(3);l3.add(3);l3.add(3);l3.add(3);
l4.add(4);l4.add(4);l4.add(4);
System.out.println("L1 size "+l1.size());
System.out.println("L2 size "+l2.size());
System.out.println("L3 size "+l3.size());
System.out.println("L4 size "+l4.size());
String array="l1";
int i=l1.size();
if(i<l2.size())
array="l2";
if(i<l3.size())
array="l3";
if(i<l4.size())
array="l4";
System.out.println("Largest Array is = "+array);
ここに出力があります
L1 size 2
L2 size 3
L3 size 4
L4 size 3
Largest Array is = l4
それはあなたが望むものですか?
于 2013-06-13T09:47:07.010 に答える
0
If you have collections that implement the interface Collection, you can just use
Collection.size();
to provide the length of the collection.
Then you can compare the results of the size() calls to find the longest collection.
You don't have to iterate over all the items in the collection.
于 2013-06-13T08:58:14.460 に答える