前言
参考教程:菜鸟教程
https://www.runoob.com/java
基本数据结构
- 分类:内置数据结构和引用数据结构
- 内置数据结构:
1.byte类型:1个字节,byte a =100,默认为0
2.short类型:2个字节,默认为0
3.int类型:4个字节,默认为0
4.long类型:8个字节,默认为0
5.float类型:4个字节,默认为0.0f
6.double类型:8个字节,默认为0.0d
7.char类型:2个字节,单个字符
8.boolean类型:true和false
- 类型转换:
1.自动类型转换:
public class ZiDongLeiZhuan{
public static void main(String[] args){
char c1='a';//定义一个char类型
int i1 = c1;//char自动类型转换为int
System.out.println("char自动类型转换为int后的值等于"+i1);
char c2 = 'A';//定义一个char类型
int i2 = c2+1;//char 类型和 int 类型计算
System.out.println("char类型和int计算后的值等于"+i2);
}
}
2.强制类型转换:(type)value type
public class QiangZhiZhuanHuan{
public static void main(String[] args){
int i1 = 123;
byte b = (byte)i1;//强制类型转换为byte
System.out.println("int强制类型转换为byte后的值等于"+b);
}
}
变量类型:
- 类变量(静态变量):在方法外的变量,有static修饰
- 实例变量:在方法外的变量,无static
- 局部变量:代码块中的变量
1.类变量:private static int a
2.实例变量:prinvate int b
3.局部变量:{private int c}
- 常量的定义:
private final String NAME='zzy'
常量的变量名,一般用大写
修饰符:
default (即默认,什么也不写): 在同一包内可见,不使用任何修饰符。使用对象:类、接口、变量、方法。
private : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类)
public : 对所有类可见。使用对象:类、接口、变量、方法
protected : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)。
注意点:一般达到封装性,成员变量使用private,方法使用public,但是自己类中的方法自己用,使用private
运算符
- 算术运算符:C语言
- 关系运算符:C语言
- 位运算符:C语言
- 逻辑运算符:C语言
- 赋值运算符:C语言
- 其他运算符:
- 三元运算符:
类型 x = (条件 )? value if true : value if false
循环
- while():
public class Test {
public static void main(String args[]) {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
- do while():
public class Test {
public static void main(String args[]){
int x = 10;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
- for:
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
- for增强语句:
public class Test {
public static void main(String args[]){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
- break,continue语句:C语言
条件
- if,if …else,if …else if …else:C语言
- switch … case语句:
public class Test {
public static void main(String args[]){
//char grade = args[0].charAt(0);
char grade = 'C';
switch(grade)
{
case 'A' :
System.out.println("优秀");
break;
case 'B' :
case 'C' :
System.out.println("良好");
break;
case 'D' :
System.out.println("及格");
break;
case 'F' :
System.out.println("你需要再努力努力");
break;
default :
System.out.println("未知等级");
}
System.out.println("你的等级是 " + grade);
}
}
Scanner类
- 首先是利用scanner,要导入包:
imoprt java.util.Scanner;
- 创建Scanner对象的基本语法:
Scanner input =new Scanner(System.in);
- next方法:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// next方式接收字符串
System.out.println("next方式接收:");
// 判断是否还有输入
if (scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
- nextLine方法:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判断是否还有输入
if (scan.hasNextLine()) {
String str2 = scan.nextLine();
System.out.println("输入的数据为:" + str2);
}
scan.close();
}
}
- next与nextLine的区别:
next():
1、一定要读取到有效字符后才可以结束输入。
2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
next() 不能得到带有空格的字符串。
nextLine():
1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空白。
- next与nextlLine接受的都是字符串,要用int和float需要使用, hasNextXxx() 方法
import java.util.Scanner;
class RunoobTest {
public static void main(String[] args) {
System.out.println("请输入数字:");
Scanner scan = new Scanner(System.in);
double sum = 0;
int m = 0;
while (scan.hasNextDouble()) {
double x = scan.nextDouble();
m = m + 1;
sum = sum + x;
}
System.out.println(m + "个数的和为" + sum);
System.out.println(m + "个数的平均值是" + (sum / m));
scan.close();
}
}
数组
一维数组
- 声明数组:存储变量
public class TestArray {
public static void main(String[] args) {
// 数组大小
int size = 10;
// 定义数组
double[] myList = new double[size];
myList[0] = 5.6;
myList[1] = 4.5;
myList[2] = 3.3;
myList[3] = 13.2;
myList[4] = 4.0;
myList[5] = 34.33;
myList[6] = 34.0;
myList[7] = 45.45;
myList[8] = 99.993;
myList[9] = 11123;
// 计算所有元素的总和
double total = 0;
for (int i = 0; i < size; i++) {
total += myList[i];
}
System.out.println("总和为: " + total);
}
}
- 处理数组:
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// 打印所有数组元素
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// 计算所有元素的总和
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// 查找最大元素
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max);
}
}
- for–each循环:
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// 打印所有数组元素
for (double element: myList) {
System.out.println(element);
}
}
}
- 数组作为函数的参数:
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
printArray(new int[]{3, 1, 2, 6, 4, 2});
- 数组作为函数的返回值
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}
多维数组
- 初始化:
String str[][] = new String[3][4];
String str[][] = new String[][]{1,1,1;2,2,2};
String str[][] = {1,1,1;2,2,2};
- 随机分配:
String s[][] = new String[2][];
s[0] = new String[2];
s[1] = new String[3];
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");
Arrays:
- 导入包:
import java.util.Arrays;
import java.util.*;
- 一些功能:
给数组赋值:通过 fill 方法。
对数组排序:通过 sort 方法,按升序。
比较数组:通过 equals 方法比较数组中元素值是否相等。
查找数组元素:通过 binarySearch 方法能对排序好的数组进行二分查找法操作。
package day01;
import java.util.Arrays;
public class 数组中的Arrays {
public static void main(String[] args) {
//toString方法让数组遍历,避免了利用for循环,
//toString得到的字符串,所以定义变量需使用字符串模型
int []number= {1,2,3,4,5};
String str=Arrays.toString(number);
System.out.println(str);
//fill给数组赋值。????
int []number1=new int[4];
for(int i=0;i<number1.length;i++) {
Arrays.fill(number1, i);
System.out.println(number[i]);
}
for(int element:number1)
{
System.out.println(element);
}
//sort对数组进行排序
int []number2=new int[]{23,54,68,12};
Arrays.sort(number2);
System.out.println(Arrays.toString(number2));
//binarySearch使用二分法查找元素在数组中的位置。
System.out.println(Arrays.binarySearch(number2, 54));
//equals表示两个数组的元素是否相同。
System.out.println(Arrays.equals(number1,number2));
}
}
- 参考链接:
https://www.runoob.com/manual/jdk11api/java.base/java/util/Arrays.html
方法(函数)
- 方法定义:
public static int age(int birthday){...}
public属于修饰符,static也是修饰符,int返回类型,age函数名,int birthday是形式参数
public class TestMax {
/** 主方法 */
public static void main(String[] args) {
int i = 5;
int j = 2;
int k = max(i, j);
System.out.println( i + " 和 " + j + " 比较,最大值是:" + k);
}
/** 返回两个整数变量较大的值 */
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
- 按值传递参数
public class TestPassByValue {
public static void main(String[] args) {
int num1 = 1;
int num2 = 2;
System.out.println("交换前 num1 的值为:" +
num1 + " ,num2 的值为:" + num2);
// 调用swap方法
swap(num1, num2);
System.out.println("交换后 num1 的值为:" +
num1 + " ,num2 的值为:" + num2);
}
/** 交换两个变量的方法 */
public static void swap(int n1, int n2) {
System.out.println("\t进入 swap 方法");
System.out.println("\t\t交换前 n1 的值为:" + n1
+ ",n2 的值:" + n2);
// 交换 n1 与 n2的值
int temp = n1;
n1 = n2;
n2 = temp;
System.out.println("\t\t交换后 n1 的值为 " + n1
+ ",n2 的值:" + n2);
}
}
- 构造方法:有参构造和无参构造,可以通过定义对象时,对成员变量进行初始化赋值
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.x + " " + t2.x);
}
}
- 其他:
package java基础;
public class 方法 {
//class中类名多个单词组合,每个单词的首字母应使用大写
//方法名中多个单词组合,第一个单词应使用小写字母开头,后面的字母使用大写字母开头
public static void main(String[] args) {
int a=1;
int b=2;
swap(a,b);
}
//方法头包括修饰符,函数类型,方法名
public static void swap(int a,int b) {
int temp=a;
a=b;
b=temp;
System.out.println(a);
}
//方法的定义,void类型没有返回值,其他类型有返回值,用return来进行返回。
//方法的调用:传入的只是值,参数没有参数过去,实参与形参的区别。
//方法的重载:方法名相同,但是方法类的参数不相同。根据参数的不同,来进行选取不同的方法。
//局部变量,成员变量,和类变量(static)。
}
- finalize()方法:
1.就是一种回收不使用的对象的方法。
2.使用System.gc和Runtime.getRuntime().gc())告诉机器尽快回收垃圾
参考文献:
https://blog.csdn.net/qq_37823003/article/details/107333386?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162349430716780255277040%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=162349430716780255277040&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-107333386.pc_search_result_control_group&utm_term=finalize&spm=1018.2226.3001.4187