/*★★
====第十三题==================================
找出一个整数数组{2,4,1,4,3,2,4,3,2}出现次数最多的数。
//找字符串中出现过多少个字符,统计每个字符出现的次数,并且把字符按照次数升序排列。
*/
public class Di13 {
public static void main(String[] args) {
int arr[] = { 2, 4, 1, 4, 3, 2, 4};
sort(arr);
}
public static void sort(int arr[]) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i : arr) {
Integer value = map.get(i);
if (value == null) {
map.put(i, 1);
} else {
value++;
map.put(i, value);
}
}
TreeSet<Charsort> set = new TreeSet<Charsort>(
Collections.reverseOrder());
// Collections.reverseOrder()是强行逆转实比较类的顺序
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
set.add(new Charsort(entry.getKey(), entry.getValue()));
}
for (Charsort ch : set) {
System.out.println(ch);
}
}
}
class Charsort implements Comparable<Charsort> {
Integer key;
Integer value;
Charsort(Integer key, Integer value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(Charsort o) {
int num = this.value.compareTo(o.value);
if (num == 0) {
return o.key.compareTo(this.key);//因为强行逆转比较器,所以这了先反转下
}
return num;
}
public String toString() {
return key + "的个数是:" + value;
}
}
/*★★★★
====第十四题==================================
给定两个字符串,比较两个字符串中最大相同的子串.
*/
public static String comStr(String s1, String s2) {
String max = null;
String min = null;
if (s1.length() >= s2.length()) {
max = s1;
min = s2;
} else {
max = s2;
min = s1;
}
for (int i = 0; i < min.length(); i++) {
for (int j = 0, k = min.length() - i; k != min.length() + 1; j++, k++) {
String str = min.substring(j, k);
if (max.contains(str))
return str;
}
}
return "没有找到";
}
/*★★★
====第十五题==================================
编写一个程序,记录该程序运行次数。运行满足5次,就提示用户“软件试用期限已到”;
*/
public static void runtime() throws Exception {
File file = new File("d:\\time.txt");
if (!file.exists()) {
file.createNewFile();
}
InputStream is = new FileInputStream(file);
OutputStream os = null;
Properties ppt = new Properties();
ppt.load(is);
String value = ppt.getProperty("counttime");
int count = 0;
if (value != null) {
count = Integer.parseInt(value);
if (count >= 5) {
System.out.println("使用次数已到,");
System.exit(0);
}
}
count++;
ppt.setProperty("counttime", count + "");
os = new FileOutputStream("d:\\time.txt");
ppt.store(os, null);// 将此 Properties 表中的属性列表(键和元素对)写入输出流。
}
/*★★★
====第十六题==================================
已知文件a.txt文件中的内容为“bcdeadferwplkou”,
请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。
即b.txt中的文件内容应为“abcd…………..”这样的顺序。
*/
public static void Test11() throws Exception
{
FileInputStream fis=new FileInputStream("c:\\a.txt");
FileOutputStream fos=new FileOutputStream("c:\\b.txt");
BufferedReader bfr=new BufferedReader(new InputStreamReader(fis));
String str=bfr.readLine();
StringBuilder sb=new StringBuilder();
TreeSet<Character> set=new TreeSet<Character>();
char []ch=str.toCharArray();
for(char c:ch)
{
set.add(c);
}
for(Character cha:set)
{
// System.out.print(cha);
sb.append(cha);
}
fos.write(sb.toString().getBytes());
}
/*★★★
====第十七题==================================
客户端向服务端上传图片。将客户端封装成线程。
*/
public class Di17_server {
public static void main(String[] args) throws Exception {
receive();
}
public static void receive() throws Exception {
ServerSocket ss = new ServerSocket(9999);
while (true) {
Socket s = ss.accept();
Rethread rt = new Rethread(s);
Thread t = new Thread(rt);
t.start();
}
}
}
class Rethread implements Runnable {
private Socket s;
int a = 0;
public Rethread(Socket s) {
this.s = s;
}
@Override
public void run() {
String postname = s.getInetAddress().getHostAddress();
FileOutputStream fos=null;
PrintWriter pw=null;
try {
InputStream is = s.getInputStream();
File file = new File("d:\\" + postname + "_" + (++a) + ".jpg");
while (file.exists())// 要使用while判断,
{
file = new File("d:\\" + postname + "_" + (++a) + ".jpg");
System.out.println(a);
}
fos = new FileOutputStream(file);
byte bytes[] = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
pw = new PrintWriter(s.getOutputStream(), true);
pw.println("上传成功");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
pw.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Di17_client {
public static void main(String[] args) throws Exception {
send();
}
public static void send() throws Exception
{
Socket s=new Socket("127.0.0.1", 9999);
OutputStream os=s.getOutputStream();
FileInputStream fis=new FileInputStream("d:\\111.jpg");
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))!=-1)
{
os.write(b, 0, len);
}
s.shutdownOutput();//告诉服务器,客户端已经上传完毕
BufferedReader bfr=new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(bfr.readLine());
}
}
/*★★★★
====第十八题==================================
在java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符。
但对应的字节数不同,一个汉字占两个字节。
定义一个方法,按照最大的字节数来取子串。
如:对于“ab你好”,如果取三个字节,那么子串就是ab与“你”字的半个,
那么半个就要舍弃。如果去四个字节就是“ab你”,取五个字节还是“ab你”.
*/注释:删除末尾问号的方法不行
public static void main(String[] args) {
String str = "ab你好";
byte[] b = str.getBytes();
String s = new String(b, 0, by(b, 5));
System.out.println(s);
}
public static int by(byte[] b, int len) {
len--;
int i = 0;
while (i <= len) {
if (b[i] < 0) {
i++;
if (i > len) {
return i - 1;
}
}
i++;
}
return i;
}
/*★★
====第十九题==================================
利用LinkedList去实现一个队列的效果.
自定该功能类.(队列的特点是元素先进先出,去查看LinkList中的方法)
*/
class MyLinkedList<T>{
private LinkedList<T> ll;
public MyLinkedList()
{
ll=new LinkedList<T>();
}
public void add(T t)
{
ll.add(t);
}
public T get(int i)
{
return ll.get(i);
}
public String toString()
{
StringBuilder sb=new StringBuilder();
for(T l:ll)
{
sb.append(l+",");
}
return "["+sb.deleteCharAt(sb.length()-1)+"]";
}
}
//先进后出
class MyLinkedList<T> {
private LinkedList<T> link;
MyLinkedList() {
link = new LinkedList<T>();
}
public void myAdd(T o) {
link.addFirst(o);
}
public Object myGet() {
return link.removeFirst();
}
public boolean isNull() {
return link.isEmpty();
}
}
/*★★★
====第二十题==================================
将一个图片切割成多个文件。在将多个文件合并成该图片
*/
public static void spilt(File file) throws Exception {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = null;
byte b[] = new byte[1024 * 1024];
int len = 0;
int a = 1;
while ((len = fis.read(b)) != -1) {
if (true) {
fos = new FileOutputStream("c:\\" + a + ".part");
fos.write(b, 0, len);
a++;
}
}
fos.close();
fis.close();
}
public static void marge() throws Exception {
Vector<FileInputStream> v = new Vector<FileInputStream>();
for (int i = 1; i < 4; i++) {
v.add(new FileInputStream("c:\\" + i + ".part"));
}
Enumeration<FileInputStream> en=v.elements();
SequenceInputStream sis=new SequenceInputStream(en);
FileOutputStream fos=new FileOutputStream("c:\\0.jpg");
byte b[]=new byte[1024];
int len=0;
while((len=sis.read(b))!=-1){
fos.write(b, 0, len);
}
fos.close();
sis.close();
}
/*★★
====第二十一题==================================
编写一个方法。去除Vector集合中的重复元素。
*/
方法1
public static <T> Vector<T> del(Vector<T> v) {
int a = v.size();
for (int i = 0; i < a; i++) {
for (int j = 0; j < i; j++) {
if (v.get(j).equals(v.get(i))) {
v.remove(i);
a--;
i--;
}
}
}
return v;
}
方法2
public static <T> Vector<T> del2(Vector<T> v) {
HashSet<T> set = new HashSet<T>();
Vector<T> v2 = new Vector<T>();
for (T ve : v) {
set.add(ve);
}
// for (int i = 0; i < v.size(); i++) {
// set.add(v.get(i));
// }
// Iterator<T> it = set.iterator();
// while (it.hasNext()) {
// v2.add(it.next());
// }
for (T s : set) {
v2.add(s);
}
return v2;
}
/*★★★★★
====第二十二题==================================
统计一个字符串中字母出现的次数。如:"aaabbbdef"
找字符串中出现过多少个字符,统计每个字符出现的次数,并且把字符按照次数升序排列。
a=3
b=3
d=1
e=1
f=1
*/
和十三题一样
-