码迷,mamicode.com
首页 > 编程语言 > 详细

java学习笔记(一)

时间:2014-11-25 18:13:55      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   os   使用   sp   java   

List使用

List lst = new ArrayList();//List是接口,ArrayList是实现类

list.add("hello");//添加

list.removeAt(0);//删除

list.get(0);//获得

Int32[] vals = (Int32[])list.toArray(typeof(Int32));//转换成数组

String[] attr = (String[])list.toArray(new String[list.size()]);//

//遍历foreach

List<String> lst = new ArrayList<String>();//泛型

lst.add("aaa");//.......

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

list1.addAll(list2);//连接list2到list1

//数组转成list

List lst = Arrays.asList("a","b","c");

String[] arr = new String[]{"hell","worl","hah"};

List lst = Arrays.asList(arr);

常用集合类的继承关系

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使用

Queue<String> que = new LinkedList<String>();

que.offer("hello");//入队列

while (que.poll() != null);//出队列

que.size();//队列大小

线程安全队列实现:http://blog.csdn.net/madun/article/details/20313269

socket udp实例

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学习笔记(一)

标签:style   blog   http   io   ar   os   使用   sp   java   

原文地址:http://www.cnblogs.com/feilv/p/4121489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!