随笔分类 - java基础
java基础
摘要:import java.lang.reflect.Constructor;
//请注意执行顺序问题,开始时,我测试时用的是MyEclipse中的Jdk1.5.Jdk1.6我发现他们Constructor的顺序不一样的。
public class ReflectTest {
String s;
int i,i2,i3;
protected ReflectTest()
{
s="";
i=0;
i2=0;
i3=0;
}
protected ReflectTest(String s,int i)
{
this.s = s;
this.i = i;
}
public ReflectTest(String...strings) throws NumberFormatException //可变数量的参数
{
if(0strings.length)
{
i = Integer.valueOf(strings[0]);
}
if(1strings.length)
{
阅读全文
摘要:import java.io.*;
public class BufferedReaderWriter {
public static void main(String[] args) {
String content[] = {"好久不见了","最近好吗","常联系"};
File file = new File("E:/workspace/Java从入门到精通/mynote.txt");
try{
FileWriter fw = new FileWriter(file);
BufferedWriter bufferfw = new BufferedWriter(fw);
for(int k=0;kcontent.length;k++){
bufferfw.write(content[k]);
bufferfw.newLine();
}
bufferfw.close();
fw.close();
}catch(Exc
阅读全文
摘要:import java.io.*;
public class FileInputOutputStream {
public static void main(String[] args) {
File file = new File("E:/workspace/Java从入门到精通/mynote.txt");
try
{
FileOutputStream out = new FileOutputStream(file);
String str = "程序设计离不开编程语言,但是编程语言在国内的大环境中似乎一直是个二等公民。国内的计算机教育和工程培训,似乎一直在宣传“语言不重要,重要的是思想”,“语言一通百通”等观点,甚至在许多人眼中“语言的讨论”完全是不入流的,但其实“编程语言”与“工具”、“框架”或是“开发方法”等事物一样,都对生产力有着重要的影响。事实上,语言的发展历史比其他方面更为悠久,并且在过去十几年,甚至最近几年中都依然在不断的碰撞,演变。期间一些新的语言诞生了,而另一些在当时看来阳春白雪的语言和编程范式也重新获得了人们的重视。";
阅读全文
摘要:import java.io.*;
public class FileTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("E:/workspace/Java从入门到精通/mynote.txt");
if(file.exists())
{
file.delete();
System.out.println("文件已经删除。");
}
else
{
try{
file.createNewFile();
System.out.println("文件已经创建。");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
阅读全文
摘要:import java.util.*; //导入类
public class MapTest {
/**
* @param args
*/
public static void main(String[] args){
Map map = new HashMap();
Emp emp = new Emp("001","张三");
Emp emp2 = new Emp("005","张四"); //创建emp对象
Emp emp3 = new Emp("004","王一");
map.put(emp.getE_id(), emp.getE_name());
map.put(emp2.getE_id(), emp2.getE_name());
map.put(emp3.getE_id(), emp3.getE_name());
Set set = map.keySet();
Iterator it = set.iterator();
System.out.println(
阅读全文
摘要:import java.util.*; //导入类
public class ListGather {
/**
* @param args
*/
public static void main(String[] args) {
List list = new ArrayList(); //集合类
list.add("a");
list.add("b");
list.add("c");
int i = (int)(Math.random()*(list.size()-1)); //0-2
System.out.println("随机获得数组中的元素:"+list.get(i));
for(int j =0;jlist.size();j++) //循环遍历集合
{
System.out.println(list.get(j));
}
}
}
阅读全文
摘要:import java.text.DecimalFormat;
import java.util.Random;
public class DecimalFormatDemo {
/**
* 简单格式化
* @param pattern
* @param value
*/
public static void SimpleFormat(String pattern,double value)
{
DecimalFormat myFormat = new DecimalFormat(pattern);
String output = myFormat.format(value);
System.out.println(value + " "+ pattern + " "+ output);
}
/**
* 使用applyPattern()方法对数字进行格式化
* @param pattern
* @param value
*/
static public void UseApplyP
阅读全文
摘要:public class JavaType {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//summation
String str[] = {"89","12","10","18"};
int sum = 0;
for(int i=0;istr.length;i++)
{
int myint = Integer.parseInt(str[i]);
sum += myint;
}
System.out.println("数组中的各种元素之和为:"+ sum);
//charac
String str1 = Integer.toString(456);
String str2 = Integer.toBinaryString(456);
String str3 = Integer.toHexString(456);
阅读全文
摘要:import java.util.Arrays;
public class JavaArray {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一维数组:
int arr[] = new int[]{1,2,3,5,15};
int arr2[] = {23,12,6};
int day[] = new int[]{31,28,31,30,31,30,31,30,31,30,31,31};
for(int i=0;i12;i++)
{
System.out.println((i+1)+"月有"+day[i]+"天");
}
//二级数组:
//int a[][] = new int[2][];
//a[0] = new int[2];
//a[1] = new int[3];
int a[][] = new int[][]
阅读全文
摘要:import java.util.Date;
public class JavaString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//char to string
char a[] = {'g','o','o','d','.'};
String s = new String (a); //全部字符
System.out.println(s);
String ss = new String (a,2,2);//部分字符
System.out.println(ss);
//多个字符串连接
String s1 = new String ("Hello");
String s2 = new String ("World");
String sss = s1 + " "+ s2;
System.out.println(sss);
阅读全文
摘要:public class GetIfElse {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//if语句
int x = 45;
int y = 12;
if(xy)
{
System.out.println("变量xy");
}
if(x30)
{
System.out.println("z30");
}else if(z10)
{
System.o
阅读全文
摘要:public class MyNumber {
static String s1="您好。";
public static void main(String[] args) {
// TODO Auto-generated method stub
String s2 = "Java";
System.out.println(s1);
System.out.println(s2);
//byte short int long
byte mybyte = 124; //-128-127
short myshort = 32564; //-32768 - 32767
int myint=45784612;
long mylong = 46789451;
long result = mybyte + myshort +myint + mylong;
System.out.println("结果为:" + result);
// float double;
float f1 = 13.23f
阅读全文