昨天程序写的比较晚,所以没来得及更新,今天补上。真是每天都是一个新的开始。主要是,似乎还是得找WEB方向的,所以一般都是招JAVA开发,对于一个0基础的我,从头学的话还是有点要花时间,不过昨天看了看SE好在和C++差不多,计划是先学SE然后开始转EE什么的。总之,因为学了很多语言的基础,再学一门新的语言,基础的话应该能够很快上手吧,大概!
一些基本数据结构
////-----------------数组排序-----------
public static void main(String[] args) {
int[] a=new int[]{1,4,2,1,6,435,132,-23};
int[] b={1,3,4,2,32,56,8};
Arrays.sort(a); //快速排序方法
for(int i:a)
System.out.println(i);
}
////-----------数组初始化------------------
public static void main(String[] args) {
int[] arr2=new int[]{1,2,3,4,5,6}; //第一种
int[] arr1={1,2,3,4,5}; //第二种
////----------------数组引用-------------------
int[] arr=arr1; //arr相当于arr1的引用,即别名
arr[1]=10; //此时arr1[1]=10
//---------------------数组拷贝值-----------
int[] copyArr=Arrays.copyOf(arr1, arr1.length);
copyArr[1]=11; //只有copyArr[1]的值被改变
for(int i:arr)
System.out.print(i+" ");
System.out.println("");
for(int i:arr1)
System.out.print(i+" ");
System.out.println("");
for(int i:arr2)
System.out.print(i+" ");
System.out.println("");
for(int i:copyArr)
System.out.print(i+" ");
////------------------大数值-------------
public static void main(String[] args) {
BigInteger a=BigInteger.valueOf(100); //将100转换为大数值
BigInteger b=a.add(a); //b=a+20
BigInteger c=a.subtract(b); //c=a-2
BigInteger d=a.multiply(c); //d=a*c
BigInteger e=a.divide(d); //e=a/d
int f=a.compareTo(e); //= 0;> + ;< -
System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f);
// }
////-----------file process------------
public static void main(String[] args) throws IOException{
Scanner in=new Scanner(Paths.get("E:\\java\\workspace\\helloworld\\bin\\a.txt"));
String name=in.nextLine();
System.out.println(name);
PrintWriter out=new PrintWriter("E:\\java\\workspace\\helloworld\\bin\\a.txt");
}
////---------------读取账号密码-Console---------- ???????????
public static void main(String[] args) {
Console cons=System.console();
String name=cons.readLine("user name: ");
Scanner in=new Scanner(System.in);
name=in.nextLine();
char[] passwd=cons.readPassword("password: ");
}
//
//---------------cin,cout-----------------
public static void main(String[] args) {
Scanner in=new Scanner(System.in); //构造一个Scanner对象,并与System.in关联
System.out.println("what is your name?");
String name=in.nextLine(); //输入一行
System.out.println("what is your sex");
String sex=in.next(); //输入一个单词以空格为分隔符
System.out.println("How old are you");
int age=in.nextInt(); //输入一个整数
System.out.println("name: "+name+"; sex: "+sex+" age: "+age);
in.close();
}
//
//----------java.lang.StringBuilder--------
public static void main(String[] args) {
StringBuilder z=new StringBuilder();
String str="i m ";
z.append(str);
z.append("reimu ");
z.insert(2,"a");
String s=z.toString();
System.out.println(s); //output i am reimu
//if z refers to a string builder object whose
//current contents are "start", then the method call z.append("le")
//would cause the string builder to contain "startle",
//whereas z.insert(4, "le") would alter the string builder
//to contain "starlet".
z.delete(5,11); //delete index: 5-10
System.out.println(z); //output i am .
}
//-------other java.lang.string-----------------
public static void main(String[] args) {
String s=" hello,world! ";
s=s.toUpperCase(); //upper
System.out.println(s);
s=s.toLowerCase(); //lower
System.out.println(s);
s=s.trim();
System.out.println(s); //delete head and end spaces
}
//-------------code units and code point-----
public static void main(String[] args) {
String s="hello";
System.out.println(s.length()); //s.length
int count=s.codePointCount(0,s.length());//实际字符数
System.out.println(count); //output 5
int cPA=s.codePointAt(0); //返回ascii码
System.out.println(cPA);
char first=s.charAt(0);
char last=s.charAt(s.length()-1);
System.out.println(first+" "+last);
}
////------------empty string:length=0;---------
public static void main(String[] args) {
String str=""; //no spaces
//str=null; if str=null,cannot use function
if(str.length()==0||str.equals("")){
System.out.println("empty string");
}
else
System.out.println("it isn't a empty string");
}
////-------------equals------yes->true;no->false------
////-----------equalsIgnoreCase-----------
public static void main(String[] args) {
String s1="hello";
String s2="HELLO";
if(s1.equalsIgnoreCase(s2)){
if(s1.equals(s2))
System.out.println("s1=s2");
else
System.out.println("ignorecase,s1=s2");
}
else
System.out.println("s1!=s2");
}
//output:ignorecase,s1=s2
////---------------substring---from a father string to getting a son string--
public static void main(String[] args) {
String s="i am lyy!";
String s1=s.substring(2); //cut 0-1,at 2 start
System.out.println(s1); //output :am lyy!
String s2=s.substring(2, 4);
System.out.println(s2); //output :am. string length:4-2=2
s2="i am";
System.out.println(s2);
}
////------------type cast---------------
public static void main(String[] args) {
double x=9.9;
int y=(int)x;
System.out.println(y); //output 9,get close x's min int
}
////------Math function--------------------
public static void main(String[] args) {
double[] x=new double[6];
double y=4;
x[0]=Math.sqrt(y);
x[1]=Math.sin(y);
x[2]=Math.PI;
x[3]=Math.E;
x[4]=Math.pow(y,2);
x[5]=Math.exp(y);
for(double i:x)
System.out.println(i);
}
////------------variable---final----------------
public static void main(String[] args) {
int val=10;
final int con=12; //it's not similar to c++,using 'final' to define a constant
val=11;
System.out.println(val+con);
}
public static void main(String[] args){
System.out.print("helloworld");
for(int i=0;i<10;i++){
System.out.print(i);
}
}
////-----------hello world--------------------------------
public stat```````
c void main(String[] args) {
String[] hello=new String[3];
hello[0]="hello ";
hello[1]="i am ";
hello[2]="lyy.";
for(String i:hello){
System.out.println(i); //自动换行
System.out.print(i); //不自动换行
}
类,继承:
//----------------------抽象类Person----------------------------
abstract public class Person {
private String Name;
public Person(String name){
Name=name;
}
public abstract String getDescription(); //抽象方法,不在这儿定义,但子类需要定义
public String getName() {
return Name;
}
}
//-------------------------学生类Student继承Person----------------------
public class Student extends Person{
private String Major;
public Student(String name,String major){
super(name); //对person进行初始化
Major=major;
}
public String getDescription(){
return "i am a student,my name is: "+super.getName()+",major is: "+Major;
}
public String getMajor() {
return Major;
}
public void setMajor(String major) {
Major = major;
}
}
//-----------------------员工类Employee继承Person------------------------
import java.util.Date;
import java.util.GregorianCalendar;
public class Employee extends Person{
private int Age;
private double Salary;
private Date Hireday;
private static int NextId=1; //属于类,不属于对象。任何一个对象都有NextId=1,即使对象不存在NextID也存在
private int Id;
public Employee(String name,int age ,double salary,int year,int month,int day){
super(name);
this.Age=age;
this.Salary=salary;
GregorianCalendar cal=new GregorianCalendar(year,month-1,day); //0表示1月
this.Hireday=cal.getTime();
}
public Employee(String name){
super(name);
}
public Employee(){
super("");
this.Age=0;
this.Salary=0;
this.Hireday=new Date();
}
//初始化块,在构造器初始化前自动运行
{
this.Id=NextId;
NextId++;
}
public String getDescription(){
return "i am a employee,my name is: "+super.getName();
}
public void raiseSalary(double byPercent){
double raise=Salary*byPercent/100;
this.Salary+=raise;
}
public int getId(){
return Id;
}
public void setId(){
this.Id=NextId;
NextId++;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public double getSalary() {
return Salary;
}
public void setSalary(double salary) {
Salary = salary;
}
public String getName() {
return super.getName();
}
public Date getHireday() {
return (Date) Hireday.clone();
}
public void setHireday(Date hireday) {
Hireday = hireday;
}
}
//----------------------经理类Manage继承Employee----------------------
import java.util.GregorianCalendar;
final public class Manage extends Employee{ //不允许其他类继承Manage
private static int BossId=1;
private double Bounds;
private int Bid;
public Manage(String name,int age ,double salary,int year,int month,int day,double bounds){
super(name);
super.setAge(age);
this.setSalary(salary);
GregorianCalendar cal=new GregorianCalendar(year,month-1,day);
this.setHireday(cal.getTime());
this.Bounds=bounds;
}
{
Bid=BossId;
BossId++;
}
public double getBounds() {
return Bounds;
}
public void setBounds(double bounds) {
Bounds = bounds;
}
public String getName()
{
return super.getName();
}
public int getBid() {
return Bid;
}
public void setBid(int bid) {
Bid = bid;
}
}
//-------------------main-----------------
import java.util.Date;
public class Helloworld {
public static void main(String[] args) {
Employee[] staff=new Employee[4];
staff[0]=new Employee("alex", 18, 2000, 2016, 2, 3);
staff[1]=new Employee("Bob", 19, 2500, 2016, 3, 2);
staff[2]=new Employee("Carl", 20, 3000, 2016, 5, 3);
staff[3]=new Employee();
for(Employee i:staff){
//i.setId();
i.raiseSalary(5);
}
for(Employee e:staff)
System.out.println("id="+e.getId()+",name= "+e.getName()+",age="+e.getAge()+",salary="+e.getSalary()+
",hireday="+e.getHireday());
Date d=staff[0].getHireday(); //若返回的是一个可变对象的引用,则容易改变封装性,一般需要加.clone()
d.setTime(d.getTime()-(long)365*24*60*60*1000);
System.out.println(staff[0].getHireday());
Manage boss=new Manage("Boss", 18, 6000, 2014, 5, 3, 500);
System.out.println("bid="+boss.getBid()+",name="+boss.getName()+",age="+boss.getAge()+
",salary="+boss.getSalary()+",bounds="+boss.getBounds()+",hireday="+boss.getHireday());
Person p1,p2;
p1=new Student("xxm", "math");
System.out.println(p1.getDescription());
p2=new Employee("gws", 19, 2000, 2016, 5, 5);
p2.getDescription();
Manage m=new Manage("ssss", 54, 15, 2015, 2, 2, 500);
System.out.println(m.getName()+" "+m.getBounds());
Employee e=new Manage("ssdq", 445, 12, 2012, 12, 12, 500);
System.out.println(e.getName()+" "+e.getDescription());
}