Jave学习笔记一

来源:互联网 发布:苹果手机美图软件 编辑:程序博客网 时间:2024/06/06 02:48

一、java的基本程序设计结构


1. java的基本注意事项

        1) java对大小写敏感;java的注释 // 行注释 ;  /* 一段的注释  */   ; /**  自动生成文档的注释*/。

        2)java程序块的格式

public class Class{     public static void main(String[] args){          //程序块     } }

 2. 数据类型

       1) 整形:表示没有小数部分的数据,允许是负数,long(8字节);int(4字节);short(2字节);byte(1字节)。其中长整型long数值必须加后缀L,十六进制数值有前缀0x,八进制数值有前缀0。java不表示任何无符号类型。

       2) 浮点类型:表示有小数部分的数值。float(4字节,后缀F);double(8字节,后缀D—默认格式)。

        表示溢出和出错的三种特殊浮点数值:正无穷大、负无穷大和NaN(不是一个数字),分别用Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY;,Double.NaN表示。

       ——Double.isNaN(temp);//表示判断temp是否为非数值

       ——数值计算中不含任何舍入误差,需要使用BigDecimal类

       3) char类型:表示单个字符,常用来表示字符常量。可以使用 \u 表示编码格式,例如\u03C0表示π。

       注意:转义字符 \u 可出现在字符常量和字符串的引号之外(其他所有转义字符均不可以)。

       4) boolean类型:false和true,用来判定逻辑条件,整形值和布尔值之间不能相互转换。

       5) 变量:变量名对大小写敏感。 

       格式 : 变量类型 变量名

int i,j; //可以在一行中声明多个变量,但会降低程序的可读性。

       注意:变量声明尽可能靠近变量第一次使用的地方。

       6) 常量

       关键字final声明常量,常量名一般用大写表示。java中某个常量一般可以在一个类的多个地方使用—类变量,用关键字 static final设置一个类常量(定义于main方法外部)。

public static final int TEMP = 2; 

       7) 运算符

       参与 / 运算得两个操作数都是整数时表示整数除法,否则表示浮点除法。浮点数被0除将得到无穷大或NaN。

       &&、|| 都是按照“短路”方式求值的。

       在处理整形数据时,可以直接对组成整形数值的各个位进行操作:& |  ^  ~分别表示与、或、异或、非。

       “>>”和“<<”运算符将二进制位进行右移和左移操作

       8) 字符串类型

package test;public class test {public static void main(String[] args){String Google = "hello wOrld";String Google2 = "hello world";//返回索引处的char值;System.out.println("Google = "+Google);System.out.println("Google.charAt(1) = "+Google.charAt(1));//返回索引出的代码点——Unicode代码点System.out.println("codePointAt = "+Google.codePointAt(4));//返回指定索引之前的字符——Unicode代码点System.out.println("codePointBefore = "+Google.codePointBefore(1));//返回指定范围中的代码点数System.out.println("codePointCount = "+Google.codePointCount(0, 2));//相同返回0 不同返回1System.out.println("compareTo = "+ Google.compareTo(Google2));//忽略大小写比较字符串,相同返回0,不同返回1System.out.println("compareToIgnoreCase = "+Google.compareToIgnoreCase("Hello World"));//将指定字符串连接到此字符串的结尾。System.out.println("concat = "+ Google.concat(" huhu!")); //当且仅当此字符串包含指定的 char值序列时,返回 true。/** * CharSequence与String都能用于定义字符串,但CharSequence的值是可读可写序列,而String的值是只读序列。 * String 继承于CharSequence,也就是说String也是CharSequence类型。 * CharSequence是一个接口,它只包括length(), charAt(int index), subSequence(int start, int end)这几个API接口。    除了String实现了CharSequence之外,StringBuffer和StringBuilder也实现了CharSequence接口。                         需要说明的是,CharSequence就是字符序列,String, StringBuilder和StringBuffer本质上都是通过字符数组实现的!  StringBuilder 和 StringBuffer都是可变的字符序列。它们都继承于AbstractStringBuilder,实现了CharSequence接口。                         但是,StringBuilder是非线程安全的,而StringBuffer是线程安全的。 * */CharSequence tempCharSequence = "hello worl";System.out.println("contains = "+Google.contains(tempCharSequence));StringBuffer tempStringBuffer = new StringBuffer("hello world");//将此字符串与指定的 StringBuffer 比较。System.out.println(Google.contentEquals(tempStringBuffer));//将此字符串与指定的 CharSequence 比较。System.out.println(Google.contentEquals(tempCharSequence));//返回指定数组中表示该字符序列的 String。char[] tempChar = {'h','l','e'};System.out.println("copyValueOf = "+Google.copyValueOf(tempChar));//返回指定数组中表示该字符序列的 String。System.out.println("copyValueOf = "+Google.copyValueOf(tempChar, 0, 2));// 测试此字符串是否以指定的后缀结束。System.out.println("endsWith = "+Google.endsWith("ld"));//将此字符串与指定的对象比较。System.out.println("equals = "+Google.equals("hello world"));//将此 String 与另一个 String 比较,忽略大小写。System.out.println("equalsIgnoreCase = "+Google.equalsIgnoreCase("HELLO WORLD"));System.out.println("format = "+String.format("%s , %s", Google,Google));//使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。byte[] tempByte = Google.getBytes();System.out.println("tempByte = " + tempByte[0]);// 将字符从此字符串复制到目标字符数组。char[] tempChar1 = {'w','o','l','d'};System.out.println(tempChar1);Google.getChars(0, 1, tempChar1, 2);System.out.println(tempChar1);//返回字符串的哈希码System.out.println(Google.hashCode());//indexof以及lastindexof类似//返回指定字符在此字符串中第一次出现处的索引。System.out.println(Google.indexOf(101)); //e-101,在字符串中的位置为1//返回指定子字符串在此字符串中第一次出现处的索引。System.out.println(Google.indexOf("ll"));//返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。System.out.println(Google.indexOf(111, 5));//返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。System.out.println(Google.indexOf("l", 5));System.out.println(Google.intern());//////////////////////////判断字符串是否为空System.out.println(Google.isEmpty());//得到字符串的长度System.out.println(Google.length());//此字符串是否匹配给定的正则表达式。//System.out.println(Google.matches());// 返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。System.out.println(Google.offsetByCodePoints(2, 2));//去除首尾空格System.out.println(Google.trim());//返回给定参数的字符串表示形式System.out.println(Google.valueOf('a'));//分割字符串String[] tempString = Google.split(" ");//测试此字符串是否以指定的前缀开始。//取代字符串System.out.println(Google.replace('o', 'k'));System.out.println("replaceAll = "+Google.replaceAll("ello", "ell"));//测试两个字符串对应区域是否相同 起始位置+另一个字符串 另一个字符串的启示位置 要比较的长度System.out.println(Google.regionMatches(0, "temp hello",5, 5));System.out.println(Google.startsWith("he"));//测试此字符串从指定索引开始的子字符串是否以指定前缀开始。System.out.println(Google.startsWith("he", 3));//返回一个新的字符序列,它是此序列的一个子序列。CharSequence tempChar12 = Google.substring(0, 2);System.out.println(tempChar12);// 返回一个新字符串,它是此字符串的一个子字符串。System.out.println(Google.substring(2));System.out.println(Google.substring(2, 6));//将此字符串转换为一个新的字符数组。char[] tempChar123 = Google.toCharArray();System.out.println(tempChar123[8]);//使用默认语言环境的规则将此 String 中的所有字符都转换为小写和大写。System.out.println(Google.toLowerCase());System.out.println(Google.toUpperCase());}}
       注意1):CharSequence是基类,可编辑,而String继承CharSequence,只读字符串。
public class CharSequence1 {public static void main(String[] args) {CharSequence tempChar = "alibaba";CharSequence tempChar2 = "alibabababa";System.out.println(tempChar.length());String tempString = tempChar.toString();System.out.println(tempString);System.out.println(tempChar.subSequence(2, 5));System.out.println(tempChar.charAt(6));}}

       2)SufferBuilder的前身是SufferBuffer,其效率略微有点低,但允许采用多线程的方式执行添加和删除字符的操作。如果所有字符串在一个单线程中编辑,应该用StringBuilder代替。

public class StringBuffer1 {public static void main(String[] args) {StringBuffer stringBuffer = new StringBuffer(20);stringBuffer.append("Goo");//末未加入字符串System.out.println(stringBuffer);stringBuffer.append(10);//末尾加入数字System.out.println(stringBuffer);System.out.println(stringBuffer.capacity());//输出容量stringBuffer.insert(2, 'c');//2号位置插入字符‘c’System.out.println(stringBuffer);}}

3. 输入和输出

1)从控制台输入:

public class inputAndOutput {public static void main(String[] args){Scanner scanner = new Scanner(System.in);//从输入流中扫描System.out.println("What is your name:");String name = scanner.nextLine();System.out.println("What is your age:");int age = scanner.nextInt();System.out.println(name+"   "+age);}}

2) 读取文件中的内容

public class FileOutput {private static BufferedReader reader;public static void main(String[] args) throws IOException {File file = new File("log.txt");InputStreamReader IN = new InputStreamReader(new FileInputStream(file),"utf-8");reader = new BufferedReader(IN);String abc ;while ((abc = reader.readLine())!=null) {System.out.println(abc);}}}

3)写入文件 :从控制台输入内容,写入文件中

public class FileInput {public static void main(String[] args) throws IOException {File file = new File("log1.txt");if (!file.exists()) {file.createNewFile();}FileOutputStream fop = new FileOutputStream(file);Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();while (!name.equals("q")) {fop.write((name+"\n").getBytes());name = scanner.nextLine();}System.exit(0);}}

4. 大数值

1)  BigInteger

BigInteger abc = BigInteger.valueOf(100);//将普通数值转化成大数值
       add ——subtract——multiply——divide——mod——compareTo

2)BigDecimal

       add:加法 ——subtract减法——multiply——compareTo

       valueOf(long x)//x的大实数     valueOf(long x,int scale)//  x / (10的scale次方)

5. 数组

int[] abc = {1,2,3,4,5};System.out.println(Arrays.toString(abc));//遍历数组中的每个元素

1)Sort

       void sort(long[] a)——long 型数组按数字升序进行排序,也可以为int[] a、float[] a、double[] a、short[] a、byte[] a、char[] a、Object[] a

int[] abc = {3,4,2,1,5};System.out.println(Arrays.toString(abc));Arrays.sort(abc);System.out.println(Arrays.toString(abc));

       void sort (Long[] a, int fromIndex, int toIndex)

int[] abc = {3,4,2,1,5};Arrays.sort(abc,1,4);

        static <T> void sort(T[] a, Comparator<? super T> c)   根据指定比较器产生的顺序对指定对象数组进行排序。

Short[] abc = new Short[]{2,3,5,3,1,8};System.out.println(Arrays.toString(abc));Comparator<Long> temp = Collections.reverseOrder();//<code><strong><a target=_blank href="http://www.766.com/doc/java/util/Collections.html#reverseOrder%28%29">reverseOrder</a></strong>()</code>返回一个比较器,它强行逆转实现了 <tt>Comparable</tt> 接口的对象 collection 的<em>自然顺序</em>。Arrays.sort(abc, temp);System.out.println(Arrays.toString(abc));

        static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)  根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。

Short[] abc = new Short[]{2,3,5,3,1,8};System.out.println(Arrays.toString(abc));Arrays.sort(abc,1,5,Collections.reverseOrder());System.out.println(Arrays.toString(abc));

2)toString

        string toString(long[] a)——返回指定数组内容的字符串表示形式,也可以为int[] a、float[] a、double[] a、short[] a、byte[] a、char[] a、Object[] a

int[] abc = {3,4,2,1,5};System.out.println(<span style="background-color: rgb(255, 153, 255);">Arrays.toString(abc)</span>);

3) int  hashCode(int[] a) ——基于指定数组的内容返回哈希码

4) equal

       static boolean  equals(int[] a, int[] a2): 如果两个指定的 int 型数组彼此相等则返回 true,也可以为int[] a、float[] a、double[] a、short[] a、byte[] a、char[] a、Object[] a

5)copyOfRange

       static int[]  copyOfRange(int[] original, int from, int to):将指定数组的指定范围复制到一个新数组

6)fill

       static void  fill(int[] a, int val):将指定的 int 值分配给指定 int 型数组的每个元素。

int[] abc = new int[6];Arrays.fill(abc, 0);System.out.println(Arrays.toString(abc));
       static void  fill(int[] a, int fromIndex, int toIndex, int val):将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。
int[] abc = {2,3,5,7,1,8};Arrays.fill(abc,1,4,0);System.out.println(Arrays.toString(abc));

7)copyOf

       static int[]  copyOf(int[] original, int newLength):复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。

int[] abc = {2,3,5,7,1,8};int[] Result = Arrays.copyOf(abc, 3);System.out.println(Arrays.toString(Result));//[2, 3, 5]int[] Result2 = Arrays.copyOf(abc, 9);System.out.println(Arrays.toString(Result2));//[2, 3, 5, 7, 1, 8, 0, 0, 0]

       static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) —— 复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。

       static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) —— 将指定数组的指定范围复制到一个新数组。

8)binarySearch

       static int binarySearch(int[] a, int key):使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。

int[] abc = {2,3,5,7,1,8};int index = Arrays.binarySearch(abc,3);System.out.println(index); //结果1,即3的
       使用二分搜索法来搜索指定数组的范围,以获得指定对象——前提是已经排好序
int[] abc = {2,3,5,3,1,8};Arrays.sort(abc);System.out.println(Arrays.toString(abc));int index = Arrays.binarySearch(abc, 3, 4, 3);System.out.println(index);

       static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) —— 使用二分搜索法来搜索指定数组,以获得指定对象。

       static<T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)  —— 使用二分搜索法来搜索指定数组的范围,以获得指定对象。

Integer[] abc = new Integer[]{8,2,4,0,1,9};System.out.println(Arrays.toString(abc));Comparator<Integer> temp = Collections.reverseOrder();Arrays.sort(abc, temp);System.out.println(Arrays.toString(abc));int tempInteger = Arrays.binarySearch(abc, 0, temp);System.out.println("tempInteger = "+tempInteger);

9)Object数组

       static boolean deepEquals(Object[] a1, Object[] a2) —— 如果两个指定数组深层相等,则返回 true。
       static int   deepHashCode(Object[] a) —— 基于指定数组的“深层内容”返回哈希码。
       static String deepToString(Object[] a) —— 返回指定数组“深层内容”的字符串表示形式。

0 0
原创粉丝点击