| |

VerySource

 Forgot password?
 Register
Search
View: 771|Reply: 5

Circulation problems;

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-8 01:30:01
| Show all posts |Read mode
as follows:
ArrayList al = new ArrayList ();
al.Add ("lei");
al.Add ("wanglei");
al.Add ("wanghao");
al.Add ("wanglei");
al.Add ("wanghao");

If I want to remove the duplicates inside, what should I do? Note that the number of values ​​in al is uncertain! ~
Reply

Use magic Report

1

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-28 12:30:02
| Show all posts
1.The removeDuplicate Method:
/ ** List order not maintained ** /
public static void removeDuplicate (ArrayList arlList)
{
   HashSet h = new HashSet (arlList);
   arlList.clear ();
   arlList.addAll (h);
}


2.The removeDuplicateWithOrder Method:
/ ** List order maintained ** /
public static void removeDuplicateWithOrder (ArrayList arlList)
{
   Set set = new HashSet ();
   List newList = new ArrayList ();
   for (Iterator iter = arlList.iterator (); iter.hasNext ();)
   {
      Object element = iter.next ()
Reply

Use magic Report

0

Threads

26

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-5-28 13:00:01
| Show all posts
It seems that there is no way to judge ~~~
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-28 14:15:01
| Show all posts
ArrayList al2 = new ArrayList ();
for (int i = 0; i <al.Count; i ++)
{
  if (! al2.Contrains (al [i])
       al2.Add (al [i]);
}

al = al2;
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-5-28 14:45:01
| Show all posts
ArrayList al = new ArrayList ();
al.Add ("lei");
al.Add ("wanglei");
al.Add ("wanghao");
al.Add ("wanglei");
al.Add ("wanghao");
al.Sort ();
for (int i = 1; i <al.Count; i ++)
{
if (al [i] .ToString () == al [i-1] .ToString ())
{
al.RemoveAt (i);
i--;
}
}

The

for (int i = 0; i <al.Count; i ++)
{
Response.Write (al [i] .ToString () + "<BR>");
}
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-5-28 20:30:01
| Show all posts
The method upstairs is good!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list