19. 发送代数据的HTTP 请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.net.URL;  
public class Main {  
public static void main(String[] args)  {  
try {  
URL my_url = new URL( "http://coolshell.cn/" );  
BufferedReader br = new BufferedReader( new InputStreamReader(my_url.openStream()));  
String strTemp = "" ;  
while ( null != (strTemp = br.readLine())){  
System.out.println(strTemp);  
}  
} catch (Exception ex) {  
ex.printStackTrace();  
}  
}  
}

20. 改变数组的大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Reallocates an array with a new size, and copies the contents
* of the old array to the new array.
* @param oldArray  the old array, to be reallocated.
* @param newSize   the new array size.
* @return          A new array with the same contents.
*/
private static Object resizeArray (Object oldArray, int newSize) {  
int oldSize = java.lang.reflect.Array.getLength(oldArray);  
Class elementType = oldArray.getClass().getComponentType();  
Object newArray = java.lang.reflect.Array.newInstance(  
elementType,newSize);  
int preserveLength = Math.min(oldSize,newSize);  
if (preserveLength > 0 )  
System.arraycopy (oldArray, 0 ,newArray, 0 ,preserveLength);  
return newArray;  
}  
// Test routine for resizeArray().  
public static void main (String[] args) {  
int [] a = { 1 , 2 , 3 };  
a = ( int [])resizeArray(a, 5 );  
a[ 3 ] = 4 ;  
a[ 4 ] = 5 ;  
for ( int i= 0 ; i
System.out.println (a[i]);  
}