java 存储只拥有基本数据类型字段的对象

AUTHOR: Locez
VERSION: 1

有时候我们需要存储一个 Java 对象的信息,以便软件在下次打开的时候还能获取到原来的属性。通常在这种情况下,我们可以实现接口 Serializable 对该类的对象进行序列化,并用 ObjectOutputStream 将对象写入文件,那么下次就可以从文件中把这个对象读取出来。但是序列化有一个问题就是静态成员不能被序列化,因为序列化是保存的对象的信息,静态成员理论上是属于类信息,因此无法采用序列化保存。

以下是我在作业过程中遇到的题目,要求将字段属性存入到文件中,然后能从文件中读取出来。于是我使用了 泛型反射,将这两个方法写成通用的了。

1
2
public static <T> void saveFields(T t, String path, char separated)
public static <T> void getFields(T t, String path, char separated)

Read More

java 实现简单的 ATM 类

首先自己也很久没有写 java ,最近给别人出了一道 java 的题目,要求也就是实现一个简单的 ATM ,当作复习,我也自己写了一个这样的 ATM 类。

  • 需要验证密码
  • 查询功能
  • 存款功能
  • 转载功能

真的 ATM 使用 6 位的数字密码,但我自己做的使用了 String ,其中一个原因是考虑到 Stringequals 方法的运用。当然为了测试,我也增加了一些自己的想法,例如缓存一个 instance 来模拟转帐操作。下面的代码,基本逻辑算是非常简单的(因此注释什么的我也没写了),但还是可以继续优化,例如判断输入是否符合 intString ,此处我只是给出一个简单的答案而已。

Read More

CodinGame-Tron Battle

IDE CodinGame

在viz的叙说下玩了一下,写了一段烂代码,但是还是有点用途的,至少不会马上就挂啊!!

Tron Battle,需要Player编写一段程序来移动,有点类似于贪吃蛇,不可以碰墙,也不可以碰自己,只能往空位的地方走,最大的不同在于Player是自己编写程序来进行移动,并且每次都有判断时限哦(100ms=0.1s),否则就TimeOut了,我这代码也会TimeOut,具体的有的地方还可以改进!这游戏如果可以的话应该是可以进行攻击以及防御的,根据对方的路线限制其行动。这样说来这其实应该和AI(人工智能)能扯上关系,有兴趣的可以去玩玩哦。

地址:www.codingame.com

上不去的可以试下翻墙!

下面贴上自己的Java版代码,写的不好,判断什么都是最简单的,实在没有时间想复杂的了!

Read More

JNI-Java Native Interface

Java提供了JNI机制,用来弥补与平台无关的特性,可以使用C/C++编写本地代码生成动态连接库交由Java调用。

首先创建一个NativeHelloWorld.java文件,包含以下内容;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//java本地方法要由native声明
public class NativeHelloWorld{
//使用静态加载块加载dll
static{
System.loadLibrary( " NativeHelloWorld " );
}
//native声明的start方法,交由c/c++实现,因此无需包含方法体;
native void start(String s);
public static void main(String[] args)
{
//实例化对象并调用start方法;
new NativeHelloWorld().start( " I am loaded by Native code of c++ " );
}
}

Read More

URLClassLoader加载类

ClassLoader提供了一个URLClassLoader实现类,该类是系统类加载器和扩展类加载器的父类。
URLClassLoader可以从本地二进制文件加载类,也可以从远程主机获取二进制文件加载类。

构造器

1
2
3
4
URLClassLoader(URL[] urls):使用parent(父类)加载器创建一个ClassLoader对象,该对象将从urls所指定的系列路径查询并获取类。

URKClassLoader(URL[] urls,ClassLoader paremt):使用指定父类加载器创建一个ClassLoader对象。

Read More

数字转换成人民币大写读法

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{
//汉字数字
public static String[] han={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
//单位
public static String[] unit={"元","十","百","千"};
//单位
public static String[] xunit={"角","分"};
//分割整数与小数部分,返回字符串数组
public static String[] divide(double num)
{
long zheng = (long)num;
long xiao = Math.round((num-zheng)*100);
if(xiao!=0)
{
return new String[]{zheng+"",xiao+""};
}
else
{
return new String[]{zheng+"",null};
}
}
//转换成汉字,4位数
public static String toHan(double number)
{
//使用divide方法得到分割后的整数与小数部分
String[] arr=Convert.divide(number);
//整数部分
String numStr=arr[0];
//小数部分
String xiaoStr=arr[1];
String result="";
//大写字符串长度
int numlen=numStr.length();
for (int i=0;i&lt;numlen;i++)
{
//在ASCII码中,字符串数字的ASCII码-48得到相应的数字ASCII码
//String.charAt(int num)方法得到指定位置的字符值
int num=numStr.charAt(i)-48;
//数字不为0,则添加单位,否则不添加
if(num!=0)
{
result+=han[num]+unit[numlen-1-i];
}
else
{
result+=han[num];
}
}
//若存在小数部分则进行处理
if(xiaoStr!=null)
{
for (int i=0;i&lt;2;i++)
{
int num=xiaoStr.charAt(i)-48;
result+=han[num]+xunit[i];
}
}
return result;
}

public static void main(String[] args)
{
System.out.println(toHan(6521.1));
}
}
/*运行结果输出如下:
陆千伍百贰十壹元壹角零分
*/

按字节截子字符串

刚刚测试的时候发现java在Linux与windows下一个中文字符所代表的字节不相同,此处是在Linux下测试的,以下作为字节参考:

windows Linux
中文 2 3
英文 1 1
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
29
30
31
32
33
34
//Substr类

public class Substr
{
//sub方法
public static String sub(String s,int start,int end)
{
//把字符串s转换成字节数组
byte[] arr=s.getBytes();
//定义一个字节数组subs,用来接收子字符串
byte[] subs=new byte[arr.length];
//使用循环,令subs字节数组接收子字节
for(int i=start,j=0;i&lt;=end;i++)
{
subs[j]=arr[i];
j++;
}
//使用subs直接用String构造函数返回子字符串
String substring = new String(subs);
return substring;
}

public static void main(String [] args)
{
String s="hello我是子字符串word";
System.out.println(s);
System.out.println("子字符串:");
System.out.println(Substr.sub(s,5,22));
}
}
/*运行输出如下:
hello我是子字符串word
子字符串:
我是子字符串*/

画圆

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
29
30
31
32
33
34

//Circle类

public class Circle

{
public static void main(String[] args)
{
//圆的半径
int r = 6;
//y为纵坐标变量
for(int y=0;y&lt;=2*r;y=y+2)
{
//求出1/2弦长
long x=Math.round(Math.sqrt(r*r-(r-y)*(r-y)));
//输出*号
for(int i=0;i&lt;=2*r;i++)
{
//i为横坐标
if(i==r-x||i==r+x)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
//转行
System.out.println("");
}
}

}