java int pos 1_长文慎点,Java学习笔记(一)

这只是一篇我自己学习java的流水账笔记,包含了以下内容,全是基础代码示例,希望对正在学习java的小伙伴有帮助

基础类型转换: The convertion of basic data types

字符串统计: String counting functions

冒泡排序,选择排序,二分法查找:bubbleSort , selectionSort & binarySearch

打印文件树:Listing files tree of a path typed from command line

java流的基本操作:Java Input/Output stream

The convertion of basic data types

char to int

When converting a char into an int , you`ll get the ASCII value of a character.

Convert character to ASCII

int ascii = (int)char;

Convert ASCII to character

ascii is an int below

char char = (char)ascii;

Example code

public class CharToAscii{

public static void main(String[] args){

System.out.println(charToAscii('a'));//97

System.out.println(asciiToChar(97));//a

}

public static int charToAscii(char c){

int ascii = c;

return ascii;

}

public static char asciiToChar(int ascii){

char c = (char)ascii;

return c;

}

}

String counting functions

First String counting

How to count a string : How many uppercase letters , lowercase letters and other symbols ? Like this:

String str = "AqSwDeFrGtgHJhhhKgfgf!@#$%^7898754";/* How many uppercase letters

lowercase letters and other symbols here ? */

Here are 3 ways to do it:

Function_1:There`s a tip about basic data convertion char to int

public static void fun_1(String s){

int lCount = 0, uCount = 0, oCount = 0;

for (int i = 0;i < s.length();i++){

char c = s.charAt(i);

if (c >= 'a' && c <= 'z'){//char to ascii

lCount++;

}else if(c >= 'A' && c <= 'Z'){

uCount++;

} else{

oCount++;

};

}

System.out.println

("There are " + lCount + "uppercase letters!" );

System.out.println

("There are " + uCount + "lowercase letters!" );

System.out.println

("There are " + oCount + "other symbols!" );

}

Function_2:Class String has the indexOf() methods

public static void fun_2(String s){

String lower = "abcdefghijklmnopqrstuvwxyz";

String upper = "ABCDEFGHIJKLMNOPKQSTUVWXYZ";

int lCount = 0, uCount = 0, oCount = 0;

for (int i = 0;i < s.length();i++){

char c = s.charAt(i);

if ( lower.indexOf(c) != -1 ){

lCount++;

}else if(upper.indexOf(c) != -1){

uCount++;

} else{

oCount++;

};

}

System.out.println

("There are " + lCount + "uppercase letters!" );

System.out.println

("There are " + lCount + "lowercase letters!" );

System.out.println

("There are " + lCount + "other symbols!" );

}

Function_3:Using the charAt() , Character.isUpperCase() and Character.isLowerCase() method

public static void fun_3(String s){

int lCount = 0, uCount = 0, oCount = 0;

for (int i = 0;i < s.length();i++){

char c = s.charAt(i);

if (Character.isLowerCase(c)){

lCount++;

}else if(Character.isUpperCase(c)){

uCount++;

} else{

oCount++;

};

}

System.out.println

("There are " + lCount + "uppercase letters!" );

System.out.println

("There are " + lCount + "lowercase letters!" );

System.out.println

("There are " + lCount + "other symbols!" );

}

Second String counting

How to count a string : How many String pointed here? Like this:

String str = "javartyjojavayyuhujavatyuuhueuhdjavajiiuhdsuhjava";

String s ="java"// How many s in str ?

Here are 2 ways to do it:

Function_1: 3 methods here : substring() , equals() , indexOf()

public static void fun_1(String str , String s){

int Count = 0, restore = 0,index = 0;

String s_start = str.substring(0,s.length());

if(s_start.equals(s)){Count = 1;}

while(index != -1 ){

index = str.indexOf(s , s.length() + restore);

if( index >= 0 ){ Count++; }

restore = index;

}

System.out.println

("\""+ s + "\"" + " appears "+ Count +"times");

}

Function_2: 2 methods here : substring() , indexOf()

public static void fun_2(String str , String s){

int Count = 0,index = -1;

while( (index = str.indexOf(s)) != -1){

str = str.substring(index + s.length());

Count ++;

}

System.out.println

("\""+ s + "\"" + " appears "+ Count +"times");

}

bubbleSort , selectionSort & binarySearch

The source code here

public class Test{

public static void main(String[] args){

Date[] d = new Date[4];

d[0] = new Date(1994,8,20);

d[1] = new Date(1992,7,12);

d[2] = new Date(2016,8,20);

d[3] = new Date(1990,6,23);

Date ds = new Date(2016,8,20);

System.out.println("Original:");

print(d);

System.out.println("class Date serached:"+ds);

System.out.println();

System.out.println("selectionSort below:");

selectionSort(d);

print(d);

System.out.println("index searched below:");

System.out.println

(binarySearch(d,ds));

System.out.println("bubbleSort below:");

bubbleSort(d);

print(d);

System.out.println("index searched below:");

System.out.println

(binarySearch(d,ds));

}

public static void print(Date[] date){

for(int i = 0;i < date.length;i++){

System.out.println(date[i]);

}

}

public static void selectionSort(Date[] date){

Date temp;

for (int i = 0;i < date.length ; i++ ){

for (int j = i;j < date.length; j++ ){

if( date[i].compare(date[j]) > 0 ){

temp = date[i];

date[i] = date[j];

date[j] = temp;

}

}

}

}

public static void bubbleSort(Date[] date){

Date temp;

for (int i = date.length - 1;i > 0 ;i-- ){

for (int j = 0;j < i;j++){

if( date[j].compare(date[j+1]) > 0 ){

temp = date[j];

date[j] = date[j+1];

date[j+1] = temp;

}

}

}

}

public static int binarySearch(Date[] date,Date d){

if (date.length == 0){return -1;}

int startPos = 0;

int endPos = date.length - 1;

int middlePos = (startPos + endPos)/2;

while (startPos <= endPos ){

if (d.compare(date[middlePos]) == 0){

return middlePos;

}

if (d.compare(date[middlePos]) > 0){

startPos = middlePos + 1;

}

if (d.compare(date[middlePos]) < 0){

endPos = middlePos - 1;

}

middlePos = (startPos + endPos)/2;

}

return -1;

}

}

class Date{

private int day,month,year;

Date(int y,int m,int d){

this.year = y;

this.month = m;

this.day = d;

}

public int compare(Date date){//ternary operator

return

year > date.year ? 1

:year < date.year ? -1

:month > date.month ? 1

:month < date.month ? -1

:day > date.day ? 1

:day < date.day ? -1 : 0;

}

public String toString(){//class Object method toString()

String s = new String(year+"-"+ month+"-"+ day);

return s;

}

}

Listing files tree of a path typed from command line

import java.io.*;

public class ListFilesTree{

public static void main(String[] args){//reading a path from command line

String path = args[0].replace(File.separatorChar,'/');

//converting separator

File file = new File(path);

fileTree(file,0);

}

public static void fileTree(File f,int level){//level denotes the depth of path

String preStr = "";

for (int i = 0;i < level;i++){

preStr += "---|";

}

File[] flist = f.listFiles();

for (int i = 0;i < flist.length;i++){

System.out.println(preStr + flist[i]);

if( flist[i].isDirectory() ){

fileTree(flist[i],level + 1);/*calling the

fileTree itself is a recursion

level increase 1 when calling fileTree everytime

*/

}

}

}

}

Java Input/Output stream

FileInputStream & FileOutputStream

import java.io.*;

public class TestFileOutputStream{

public static void main(String[] args){

int b = 0;

FileInputStream in = null;

FileOutputStream out = null;

String inPath =

"D:\\Important files\\JAVA\\practice\\IO\\TestFileOutputStream.java";

String outPath =

"D:\\Important files\\JAVA\\practice\\IO\\CopyTestFileOutputStream.java";

try{

in = new FileInputStream(inPath.replace(File.separatorChar,'/'));

out = new FileOutputStream(outPath.replace(File.separatorChar,'/'));

while ( (b = in.read()) != -1){

out.write(b);

System.out.print((char)b);

}

in.close();out.close();

}catch(FileNotFoundException e){

System.out.println("File not found!");

System.exit(-1);

}catch (IOException e1){

System.out.println("io error!");

System.exit(-1);

}

System.out.println("文件已经复制");

}

}

FileReader & FileWriter

import java.io.*;

public class TestFileReader{

public static void main(String[] args){

int c = 0;

FileReader fr = null;

FileWriter fw = null;

String frPath =

"D:\\Important files\\JAVA\\practice\\IO\\TestFileReader.java";

String fwPath =

"D:\\Important files\\JAVA\\practice\\IO\\copyTestFileReader.java";

try{

fr = new FileReader(frPath.replace(File.separatorChar,'/'));

fw = new FileWriter(fwPath.replace(File.separatorChar,'/'));

while ((c = fr.read()) != -1 ){

fw.write(c);

System.out.print((char)c);

}

fr.close();

fw.close();

}catch(FileNotFoundException e){

System.out.println("文件找不到!");

System.exit(-1);

}catch(IOException e1){

System.out.println("文件错误!");

System.exit(-1);

}

System.out.println("文件已复制!");

}

}

import java.io.*;

public class TestFileWriter{

public static void main(String[] args){

FileWriter fw = null;

try{

fw = new FileWriter

("D:\\Important files\\JAVA\\practice\\IO\\Unicode.dat");

for (int c = 0;c <= 50000;c++){

fw.write(c);

}

fw.close();

}catch(FileNotFoundException e){

System.out.println("文件找不到!");

System.exit(-1);

}catch(IOException e1){

System.out.println("IO错误!");

System.exit(-1);

}

}

}

Buffered Stream

import java.io.*;

public class TestBuffered{

public static void main(String[] args){

try{

FileInputStream in = new FileInputStream

("D:\\Important files\\JAVA\\practice\\IO\\TestBuffered.java");

BufferedInputStream bis = new BufferedInputStream(in);

System.out.println((char)bis.read());

System.out.println((char)bis.read());

int c = 0;

for(int i = 0;i <= 100 && (c = bis.read()) != -1;i++ ){

System.out.print((char)c+"");

}

}catch(IOException e){e.printStackTrace();}

}

}

import java.io.*;

public class TestBufferWriter{

public static void main(String[] args){

try{

BufferedWriter bw = new BufferedWriter

(new FileWriter("D:\\Important files\\JAVA\\practice\\IO\\Test\\1.txt"));

BufferedReader br = new BufferedReader

(new FileReader("D:\\Important files\\JAVA\\practice\\IO\\Test\\1.txt"));

String s = null;

for(int i = 0;i < 10;i++){

s = String.valueOf(Math.random());

bw.write(s);

bw.newLine();

}

bw.flush();

while( (s = br.readLine()) != null){

System.out.println(s);

}

bw.close();br.close();

}catch(IOException e){e.printStackTrace();}

}

}

Converting Stream

import java.io.*;

public class Transform{

public static void main(String[] args){

try{

String path =

"D:\\Important files\\JAVA\\practice\\IO\\Test\\transform.txt";

OutputStreamWriter osw = new OutputStreamWriter

(new FileOutputStream(path));

osw.write("output stream writer transforms the file output stream!");

System.out.println(osw.getEncoding());

osw.close();

osw = new OutputStreamWriter

((new FileOutputStream(path,true)),"ISO8859_1");

osw.write("output stream writer transforms the file output stream!");

System.out.println(osw.getEncoding());

osw.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

import java.io.*;

public class Transform1{

public static void main(String[] args){

InputStreamReader isr = new InputStreamReader(System.in);//阻塞式的方法

BufferedReader br = new BufferedReader(isr);

String s = null;

try {

s = br.readLine();

while(s != null){

if (s.equalsIgnoreCase("exit")){System.exit(-1);}

System.out.println(s.toUpperCase());

s = br.readLine();

}

br.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

Data Stream

import java.io.*;

public class DataStream{

public static void main(String[] args){

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DataOutputStream dos = new DataOutputStream(baos);

try {

dos.writeDouble(Math.random());

dos.writeBoolean(true);

ByteArrayInputStream bais = new ByteArrayInputStream

(baos.toByteArray());

System.out.println(bais.available());

DataInputStream dis = new DataInputStream(bais);

System.out.println(dis.readDouble());

System.out.println(dis.readBoolean());

}catch (IOException e){e.printStackTrace();}

}

}

Printing Stream

import java.io.*;

public class TestPrintStream1{

public static void main(String[] args){

String f = args[0];

if (f != null){

list(f,System.out);

}

}

public static void list(String f,PrintStream ps){

try{

BufferedReader br = new BufferedReader

(new FileReader(f));

while( br.readLine()!= null){

ps.println(br.readLine());

}

br.close();

}catch (IOException e){e.printStackTrace();}

}

}

import java.io.*;

import java.util.*;

public class TestPrintStream2{

public static void main(String[] args){

BufferedReader br = new BufferedReader

(new InputStreamReader(System.in));

try{

String s = null;

FileWriter fw = new FileWriter

("D:\\Important files\\JAVA\\practice\\IO\\Test\\log.txt",true);

PrintWriter log = new PrintWriter(fw);

while( (s = br.readLine()) != null){

if(s.equalsIgnoreCase("exit")) break;

System.out.println(s.toUpperCase());

log.println("-------");

log.println(s.toUpperCase());

log.flush();

}

log.println("===="+new Date()+"=====");

log.flush();

log.close();

}catch(IOException e){e.printStackTrace();}

}

}

Object Stream

import java.io.*;

public class TestObjectStream{

public static void main(String[] args){

T t = new T();

t.i = 8;

try{

FileOutputStream fos = new FileOutputStream

("D:\\Important files\\JAVA\\practice\\IO\\Test\\object.dat");

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(t);

oos.flush();

oos.close();

FileInputStream fis = new FileInputStream

("D:\\Important files\\JAVA\\practice\\IO\\Test\\object.dat");

ObjectInputStream ois = new ObjectInputStream(fis);

T tread = (T)ois.readObject();

System.out.println

(tread.i+" "+tread.j+" "+tread.d+" "+tread.k);

}catch(IOException e ){e.printStackTrace();

}catch(ClassNotFoundException e){e.printStackTrace();}

}

}

class T implements Serializable{

int i = 10;

int j = 9;

double d = 0.12324;

transient int k = 15;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值