![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
java
文章平均质量分 60
iceryan
这个作者很懒,什么都没留下…
展开
-
我理解java的多态
/* * 父类Animal,定义了三个方法, * 其中eat(),scream()是等着 * 被子类重写的,animalFun() * 是Animal自身的方法 */class Animal { public void eat() { System.out.println("我们吃各种各样的食物"); } public void scream() { System.o原创 2013-01-25 00:36:19 · 706 阅读 · 0 评论 -
IO中的内存流
ByteArrayInputStream 、 ByteArrayOutputStream数据的来源不一定是文件,也可能是数据中的一块内存练习:将字符串中的小写都转成大写再输出 //将String中的字符都转为大写 public static void main(String[] args){ String s = "a b c d e f g"; ByteArrayInput原创 2013-02-02 17:29:34 · 2285 阅读 · 0 评论 -
IO中的字符流练习
1. FileWriter FileReader 用字符流来读写文写,一次性读取2个字节 public static void main(String[] args){ File f = new File("d:\\dmeo.txt"); String s = "I Am Learning Java,我正在学习Java"; //将数据写入文件 try { File原创 2013-02-02 17:14:48 · 476 阅读 · 0 评论 -
【算法练习】数组的反转
//反转数组,找到中间的位置。将首尾的数据交换,循环centerIndex次 public static int[] reverse( int[] a){ int centerIndex = a.length/2; int start = 0; int tail = a.length -1; int temp = 0; for原创 2013-02-08 14:54:13 · 548 阅读 · 0 评论 -
【设计模式练习】单例构造
单例模式的意思是,只能构造出一个对象,如现实中只会有一个太阳,一个地球一样。不会出现两种重复的对象、还如windows的回收站,虽然在每个盘中都有回收站这个对象,但实际上无论在哪删除一个文件,在其他任意盘的回收站都可以看到。class Singleton { // 通过静态属性的方式来生成对象 private static final Singleton INSTANCE原创 2013-02-17 21:17:21 · 533 阅读 · 0 评论 -
【设计模式练习】多例设计
class Color { private String color; //在static区就构造好对象 private static final Color RED = new Color("红色"); private static final Color GREEN = new Color("绿色"); private static final Color BLUE = new Co原创 2013-02-17 22:13:56 · 504 阅读 · 0 评论 -
【算法练习】二叉树方法对数组进行排序
package com.ryan;class Node { // 定义左子树,右子树节点 private Node left; private Node right; private int data; // 需要存放的数据 public Node() { } // 通过构造函数初始化data public Node(int data) { this.data = da原创 2013-02-16 21:56:14 · 2159 阅读 · 0 评论 -
模拟链表
package com.ryan;class Node { // 表示节点,为了封装数据,并可以指向下一个节点 private Person p; private Node next; public Node(Person p) { this.p = p; } public Node getNext() { return next; } public Person原创 2013-02-13 22:45:05 · 546 阅读 · 0 评论 -
【算法练习】数组排序
1. 冒泡排序前面两个数进行比较,如果前一个数小于后一个数则进行换拉 //选择排序 public static void sort(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length-1; j++) { if (arr[j] > arr[j+1]) {原创 2013-02-08 11:47:50 · 629 阅读 · 0 评论 -
对象clone
对象clone需要此类实现Cloneable接口,重写clone()方法package com.ryan;//一个简单的人员类,只有姓名和年龄public class Person implements Cloneable{ private String name; private int age; public Person(String name ,int age){原创 2013-02-03 20:21:43 · 826 阅读 · 0 评论 -
匿名内部类
//假设有一接口A,但是此接每次只会用一次//所以就没有必要用一个特定的类来去实现它//可以使用匿名内部类interface A { public void print();}class B { public void fun(A a) { // 需要一个A的对象,但是A又是一个接口不能被实例化 a.print(); } public void get() { thi原创 2013-02-21 22:54:30 · 531 阅读 · 0 评论 -
日期格式处理
利用SimpleDateFormat将字符串转为日期格式,也可以将日期转为字符串通过parse(),可以将字符口串转为日期通过format(),可以将日期转为字符串 public static void main(String[] args){ String s = "2013-2-2 20:54:30"; SimpleDateFormat dateForamt = new S原创 2013-02-02 21:54:41 · 597 阅读 · 0 评论 -
【算法练习】二分法查询
public class Test { /** * @param int[] a 要查询的数组 * @param b 需要查询数据 */ public static int binarySearch(int[] a , int num){ int startIndex = 0; int endIndex = a.length-1; if(a.length == 0原创 2013-02-01 11:55:35 · 482 阅读 · 0 评论 -
对象的比较,排序,重写equals(),compareTo(),hashCode()方法
import java.util.ArrayList;import java.util.Collections;public class Person implements Comparable{ private String name; private int age; private String sex; public Person(String name ,int age,原创 2013-02-01 21:42:55 · 2058 阅读 · 0 评论 -
Java中自动装箱、拆箱的例子
public class Test { public static void main(String[] args) { //手动装箱,拆箱 int x = 10; Integer a = new Integer(x); //将int型变为Integer对象 int y = a.intValue(); //将Interger对象变为int型 System.out.prin原创 2013-01-25 16:08:09 · 732 阅读 · 0 评论 -
接口介绍
/* * 接口定义了某一个模块的标准 * 想使用此接口必需实现这些方 * 法。如USB,我们插入USB的时候 * 都需要安装完驱动才能开始工作 */interface USB { public void installDriver(); public void start(); public void stop();}/* * 电脑定义了这样的接口,当USB插入的时候原创 2013-01-25 15:30:57 · 473 阅读 · 0 评论 -
为什么要用泛型
有这么一个需求希望可以输出一个点的坐标,做为一个点的坐标,可以使用整型(1,2)也可以使用float型(1.0,2.0)当然也可以使用字符串型(北纬30度,东经120度)/* * 可以根据转入参数的内型不同 * 重载getPoint()方法 * */public class Point { public String getPoint(int x , int y原创 2013-01-26 01:31:19 · 1164 阅读 · 0 评论 -
抽象类的应用
/* * 抽象类的应用主要应用于模板化的设计,在父类定义 * 共有的属性和方法,抽像出来的方法等待子类来自已 * 实现。此例中,定义了一个人的类,具有name和age * 属性,并提供抽象方法getInfo(),每个子类的信息 * 不一样就让子类自已去完成,就好比,发了一个人员 * 表格,每个人自已向里面填入信息 */abstract class Person { private原创 2013-01-25 13:10:03 · 1374 阅读 · 0 评论 -
利用FileReader和FileWriter完成一个文件拷贝功能
目标:在cmd中可以执行java XXXX d:\1.txt d:\2.txt完成文件的拷贝命令package com.ryan.io;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;原创 2013-01-27 22:29:05 · 3231 阅读 · 0 评论 -
左移和右移计算
正数的左移和右移以3为例3的二进制为00000011右移2位的时候将最右的11去掉左边补00结果为:00000000 --- >十进是中的0左移2位的时候将最左的00去掉右边补00结果为:00001100 ----->十进是中的12对于-3来说,负数的都是以补码的形式存在于计算器中补码 = 反码+1-3的二进制为100000011取反的补码为(符号为原创 2013-01-28 23:32:54 · 9885 阅读 · 5 评论 -
字符串分割成二维数组,或加入到Map中
/** * 将字符串分割然后存到一个 * 二维数组中[{A,80},{B,60},{C,70}] * @return 返回值为一个二维数组 */ public static String[][] splitStringToArrary() { String s = "A:80,B:60,C:70"; String[] temp = s.split(",");原创 2013-01-29 20:00:39 · 7608 阅读 · 0 评论 -
【算法练习】递归算法的例子
一些常用的计算 //从1加到n的各,指定出口为sum(1)的时候==1 public static int sum(int n){ if (n==1) { return 1; }else{ return n+sum(n-1); } } // 求n的阶乘,指定出口为factorial(1)的时候==1 public static double factorial(i原创 2013-01-29 11:29:26 · 856 阅读 · 0 评论 -
String常用知识点小结
1. String中的==是引用的比较,equals是值的比较 public static void main(String[] args) { String a ="hello"; String b ="world"; String c = "helloworld"; String d = a+b; String z = "hello"; System.原创 2013-01-30 19:04:56 · 793 阅读 · 0 评论 -
IO中的字节流练习
1. FileInputStream FileOutputStream通过字节流来读写文件 public static void main(String[] args) throws Exception { //将文件里写数据 File f = new File("d:\\dmeo.txt"); FileOutputStream output = new FileOutput原创 2013-02-02 15:56:48 · 1710 阅读 · 0 评论 -
使用BigDecimal完成小数点后的精确位数的四舍五入
package com.ryan;import java.math.BigDecimal;class MyMath { public static double round(double num ,int scale) { BigDecimal big = new BigDecimal(num); BigDecimal result = big.divide(new BigDe原创 2013-02-23 16:16:36 · 11791 阅读 · 0 评论