Java Learning Notes (i)

Source: Internet
Author: User

List uses

List lst = new ArrayList ();//list is an interface, ArrayList is an implementation class

List.add ("Hello");//Add

List.removeat (0);//delete

List.get (0);//Acquired

Int32[] Vals = (int32[]) List.toarray (typeof (Int32));//convert to an array

String[] attr = (string[]) List.toarray (new String[list.size ()]);//

Traversing a foreach

list<string> lst = new arraylist<string> ();//Generics

Lst.add ("AAA");//....

for (String str:lst) {...}

List1.addall (LIST2);//Connect List2 to List1

The array is turned into a list

List lst = arrays.aslist ("A", "B", "C");

string[] arr = new string[]{"Hell", "worl", "hah"};

List lst = arrays.aslist (arr);

Inheritance relationship of common collection classes

Collection<--list<--vector

Collection<--list<--arraylist

Collection<--list<--linkedlist

Collection<--set<--hashset

Collection<--set<--hashset<--linkedhashset

Collection<--set<--sortedset<--treeset

Map<--sortedmap<--treemap

Map<--hashmap

Queue usage

Queue<string> que = new linkedlist<string> ();

Que.offer ("Hello");//into the queue

while (que.poll () = null);//OUT queue

Que.size ();//Queue Size

Thread-Safe Queue implementation: http://blog.csdn.net/madun/article/details/20313269

Socket UDP Instance

Import java.net.*;
public class Udpsend {
public static void Main (string[] args) throws Exception {
Datagramsocket ds = new Datagramsocket ();
byte[] buf = "UDP Demo". GetBytes ();
Datagrampacket DP = new Datagrampacket (buf, Buf.length, Inetaddress.getbyname ("127.0.0.1"), 10000);
Ds.send (DP);
Ds.close ();
SYSTEM.OUT.PRINTLN ("Data send ok!");
}
}

Import java.net.*;
public class Udpreceive {
public static void Main (string[] args) throws Exception {
Datagramsocket ds = new Datagramsocket (10000);
byte[] buf = new byte[1024];
Datagrampacket dp =new datagrampacket (BUF, buf.length);
Ds.receive (DP);
String IP = dp.getaddress (). gethostaddress ();
String data = new String (Dp.getdata (), 0, Dp.getlength ());
int port = Dp.getport ();
SYSTEM.OUT.PRINTLN (Data + ":" + port + "@" + IP);
Ds.close ();
}
}

Java Learning Note (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.