黑马程序员技术交流社区

标题: 每日学五点知识总结(五) [打印本页]

作者: abc784990536    时间: 2015-1-1 12:08
标题: 每日学五点知识总结(五)
21. Collection框架中实现比较要实现什么接口?
答:要实现Comparable接口,并实现这个接口的唯一方法cpmparaTo(),接受一个Object对象,在这个方法中可以定义对象的排序规则。

22. 介绍Java中的Collection 框架结构 , 并画出来。
〈〉Collection
〈〉Set
〈〉List
HashSet
Tree set
ArrayList
Vector

23. 编程题:列出某文件夹下的所有文件 (文件夹从命令行输入)。
解:

import Java.io.*;  
public class listFile  
{  
public static void main (String[] args)  
{  
String s=““;  
InputStreamReader ir=new InputStreamReader(System.in);  
BufferedReader in = new BufferedReader(ir);  
try {  
s = in.readLine();  
File f=new File(s);  
File[] files=f.listFiles();  
for(int i=0;i  
{  
if(files[i].isFile())  
{  
System.out.println(“文件:“+files[i]);  
}  
else
{  
System.out.println(“目录:“+files[i]);  
}  
}  
in.close();  
}  
catch (IOException e)  
{  
e.printStackTrace();  
}  
}  
}
24. 编程题::写一个满足Singleton模式的类出来。

public class SingletonTest  
{  
private static SingletonTest sp;  
private SingletonTest() {}  
public static SingletonTest getInstance()  
{  
if (sp==null)  
{ sp=new SingletonTest(); }  
return sp;  
}
25. 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
解:

import Java.io.*;  
class interceptString  
{  
String interceptStr;  
int interceptByte;  
public interceptString(String str,int bytes)  
{  
interceptStr=str;  
interceptByte=bytes;  
System.out.println(“字符串为:’“+interceptStr+“’;字节数为:“+interceptByte);  
}  
public void interceptIt()  
{  
int interceptCount; interceptCount=(interceptStr.length()%interceptByte==0)?        (interceptStr.length()/interceptByte):   (interceptStr.length()/interceptByte+1);  
System.out.println(“截取后断数为:“+interceptCount);  
for (int i=1;i〈=interceptCount ;i++ )  
{ if (i==interceptCount)  
{  
System.out.println(interceptStr.substring((i-1)*interceptByte,interceptStr.length()));  
} else
{  
System.out.println(interceptStr.substring((i-1)*interceptByte,(i*interceptByte)));  
}  
}  
}  
public static void main(String[] args)  
{  
String s=““;  
InputStreamReader ir=new InputStreamReader(System.in);  
BufferedReader in = new BufferedReader(ir);  
try {  
s = in.readLine();  
interceptString ss = new interceptString(s,4);  
ss.interceptIt();  
in.close();  
} catch (IOException e)  
{ e.printStackTrace();}  
}  
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2