算法

《统计字符》
package com.test;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;


public class demo {
public static void main(String[] args) throws Exception {
//统计每个字符的个数
String str="我们我我我员";
Map<Character,Integer> map=new HashMap<Character,Integer>();
for (int i = 0; i < str.length(); i++) {
char strchar=str.charAt(i);
Integer num=map.get(strchar);
if(num==null){
num=1;
}else{
++num;
}
map.put(strchar, num);
}
for (Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + "---" + entry.getValue());
}
System.out.println("-------------------分割线---------------------");
//统计字符
Map<String,Integer> map1=new HashMap<String,Integer>();
File file=new File("e://aaa.txt");
InputStream is=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=null;
while ((line = br.readLine()) != null) {
String[] strs = line.split(",");
String name = strs[1];
Integer count = map1.get(name);
if (null == count)
count = 1;
else
++count;
map1.put(name, count);
}
for (Entry entry1 : map1.entrySet()) {
System.out.println(entry1.getKey() + "---" + entry1.getValue());
}
}
}


《各种排序》
package com.test;
import java.util.Arrays;
import org.junit.Test;


public class demo1 {
// 递归1
public int printNum(int n) {
System.out.println(n);
int result = n;
if (n <= 5000)
result = printNum(n * 2);
System.out.println(n);
return result;
}
@Test
public void testDg() {
int i = 1237;
int d = printNum(i);
System.out.println("----" + d);
}
// 递归2
public int getAge(int num) {
int result;
if (num == 1)
result = 10;
else
result = getAge(num - 1) + 2;
return result;
}
@Test
public void testAge() {
int d = getAge(3);
System.out.println(d);
}
// 快速升序排序
public void arraySort(int[] a) {
Arrays.sort(a);
}
@Test
public void testArraySort() {
int[] a = { 1, 2, 6, 3, 5 };
arraySort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
// 冒泡
public void maopao(int[] a) {
// 外层n-1,内层n-1-i
int leng = a.length;
for (int i = 0; i < leng - 1; i++) {
int temp;
for (int j = 0; j < leng - 1 - i; j++) {
if (a[j + 1] < a[j]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
@Test
public void testMaopao() {
int[] a = { 1, 2, 6, 3, 5 };
maopao(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
//数组翻转
public void con(int[] a){
int len=a.length;
for (int i = 0; i < len/2; i++) {
int temp=a[i];
a[i]=a[len-1-i];
a[len-i-1]=temp;
}
}
@Test
public void testCon(){
int[] a={2,3,1,4,6,5};
System.out.println("反转前"+Arrays.toString(a));
con(a);
System.out.println("反转后"+Arrays.toString(a));
}
}
《文件复制》
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class Jad2Java {
public static void main(String[] args) throws IOException{
File txtFile = new File("e://old");// 旧文件存放目录
if (!txtFile.exists())
System.out.println("目录不存在");


// 一下代码,达到把txt结尾的文件放在files数组中
File[] files = txtFile.listFiles(new FileFilter() {
public boolean accept(File onefile) {
String oneFileName = onefile.getName();
return oneFileName.endsWith(".txt");
}
});
// 要复制到的目标目录,文件夹
String newFilePath = "e://newFile";
String newFileName = "";
File saveFile = new File(newFilePath);
if (!saveFile.exists() && !saveFile.isDirectory())
saveFile.mkdir();
// files数组,存放待复制的文件
for (int i = 0; i < files.length; i++) {
File file = files[i];


newFileName = file.getName().replace(".txt", ".java");
// 把一个文件,初始化成输入流
FileInputStream fis = new FileInputStream(file);

// 把要保存的目标文件,初始化成输出流 D://newFile//asdfasd.java
FileOutputStream fos = new FileOutputStream(newFilePath + "//"
+ newFileName);
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) > 0) {
fos.write(b, 0, len);
}
fos.close();
fis.close();
}
}
}
《单例模式》


package Singleton;
/**
 * 飽汗模式
 * @author lenovo
 *
 */


/*public class Singleton {


private Singleton(){}
private final static Singleton instance = new Singleton();
public static Singleton getInstance(){
return instance;
}
}*/
/**
 * 饥汉模式
 */


public class Singleton{
private Singleton(){}
private static Singleton instance =null;

public static synchronized Singleton getInstance(){
if(instance ==null)
instance = new Singleton();
return instance;
}
}
 

    /**
    *冒泡排序
    *
    /
    public static BigDecimal[] bubbleSort(BigDecimal[] sort) {
        for(int i =0;i < sort.length - 1;i++)
        {
            for(int j = 0;j <  sort.length - 1-i;j++)// j开始等于0,
            {
                if(sort[j].compareTo(sort[j+1]) < 0)
                {
                    BigDecimal temp = sort[j];
                    sort[j] = sort[j+1];
                    sort[j+1] = temp;
                }
            }
        }
        return sort;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值