一.数组
1.数组定义的方法
(1) . int []array1=new int[10];
(2). int[] array4=new int[]{0,1,2,3,4,5,6,7,8,9};
2. for each循环
String []array6=new String[]{"C语言","Java","Python"};
for(String a:array6)
{
System.out.println(a);
//输出换行
}
int[] array4=new int[]{0,1,2,3,4,5,6,7,8,9};
for(int i:array4)
{
System.out.println(i);//要注意这里是i而不是array4[i]
}
3.数组可以直接使用 数组名.length()方法的
4.这里涉及到如何输出数组元素不换行,正常来说 System.out.println();会自动换行
for(String a:array6)
{
System.out.print(a+" ");
//输出不换行的代码用print
}
5.Array.的所有方法:
(1.Array.toString()
: 将数组转换为字符串形式输出。
int[] array = {1, 2, 3, 4, 5};
String arrayString = Arrays.toString(array);
System.out.println(arrayString); // 输出: [1, 2, 3, 4, 5]
(2).Array.copyOf()
: 复制数组并返回新数组。
int[] sourceArray = {1, 2, 3, 4, 5};
int[] newArray = Arrays.copyOf(sourceArray, sourceArray.length);
for(int i=0;i<newArray.length;i++)
{
System.out.print(newArray[i]+" ");
}
(3).Array.copyOfRange()
: 复制指定范围内的数组元素并返回新数组。
int[] sourceArray1 = {1, 2, 3, 4, 5};
int[] newArray1 = Arrays.copyOfRange(sourceArray, 1, 4); // 复制索引1到3的元素
for(int i=0;i<newArray1.length;i++)
{
System.out.print(newArray1[i]+" ");
}
(4).Array.fill()
: 将数组的所有元素设置为指定的值。
int[] array = new int[5];
Arrays.fill(array, 10); // 将数组的所有元素设置为10
(5).Array.sort()
: 对数组进行升序排序。
int[] array = {5, 2, 8, 1, 3};
Arrays.sort(array); // 对数组进行升序排序
(6).Array.binarySearch()
: 在已排序的数组中搜索指定元素,并返回其索引。
int[] array = {1, 2, 3, 4, 5};
int index = Arrays.binarySearch(array, 3); // 在数组中搜索元素3
(7).Array.equals()
: 比较两个数组是否相等。
int[] array3 = {1, 2, 3};
int[] array4 = {1, 2, 3};
boolean isEqual = Arrays.equals(array3, array4); // 比较两个数组是否相等
System.out.println(isEqual);
8.Array.asList()
: 将数组转换为 List
集合。
String[] array5 = {"apple", "banana", "orange”};
List<String> list = Arrays.asList(Arrays.toString(array5)); // 将数组转换为List集合
for(int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
9.import java.util.Arrays;
二.输入
(1)hasNextInt()的用法
import java.util.Scanner;
public class exercise1217 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int num = scanner.nextInt();
System.out.println("用户输入的整数是: " + num);
}
scanner.close();
}
}
(2).hasNextLine的用法
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入多行文本(输入'q'结束):");
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equalsIgnoreCase("q")) {
break;
}
System.out.println("用户输入的文本是: " + line);
}
scanner.close();
}
}
三.重载(Overloading)和重写(Overriding)是面向对象编程中的两个概念,用于实现多态性和代码复用。
1.重载(Overloading):指在同一个类中,通过修改方法的参数列表,定义具有相同名称但具有不同参数的多个方法。重载方法可以有不同的返回类型,但仅根据参数列表的差异进行区分。
2.重写(Overriding):指在子类中重新实现(覆盖)从父类继承的方法,使得子类可以根据需要改变方法的行为。重写方法具有相同的名称、参数列表和返回类型
3.重载方法发生在同一个类中,通过修改方法的参数列表来定义不同的方法。
4.重写方法发生在子类中,对从父类继承的方法进行重新实现。
5.重载方法的调用是根据传递给方法的参数列表来选择合适的方法,根据参数类型和数量来匹配调用。
6.重写方法的调用是基于对象的具体类型,在运行时动态绑定,根据对象的实际类型选择调用哪个方法。
7.重载方法之间是独立的,它们在同一个类中,彼此没有直接的继承或覆盖关系。
8.重写方法是子类对父类方法的重新实现,子类通过继承父类方法来进行重写。
四.动态绑定
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat meows");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog barks");
}
}
public class DynamicBindingExample {
public static void main(String[] args) {
Animal animal1 = new Cat();
Animal animal2 = new Dog();
animal1.makeSound(); // 输出:Cat meows
animal2.makeSound(); // 输出:Dog barks
}
}
解释:由于 animal1 指向 Cat 对象,所以调用 animal1.makeSound() 会动态绑定到 Cat 类的 makeSound() 方法,输出 "Cat meows"。而 animal2 指向 Dog 对象,因此调用 animal2.makeSound() 会动态绑定到 Dog 类的 makeSound() 方法,输出 "Dog barks"。
五.静态方法和实例方法的使用:
class MyClass {
public static int staticField; // 静态字段
public static void staticMethod() { // 静态方法
System.out.println("This is a static method.");
}
public void instanceMethod() { // 实例方法
System.out.println("This is an instance method.");
}
}
public class Main {
public static void main(String[] args) {
// 访问静态字段和调用静态方法
MyClass.staticField = 10;
System.out.println("Static Field: " + MyClass.staticField);
MyClass.staticMethod();
// 实例化对象并访问实例方法
MyClass obj = new MyClass();
obj.instanceMethod();
}
}
输出:
Static Field: 10
This is a static method.
This is an instance method.
原因:在Java中,静态方法可以直接通过类名调用,而不需要实例化对象。因此,可以使用类名加点操作符访问静态字段和调用静态方法。对于实例方法,需要先实例化类的对象,然后通过对象引用来调用实例方法。
六.Java二维数组
public class TwoDimensionalArrayExample {
public static void main(String[] args) {
// 声明和初始化一个3x3的二维数组
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// 输出二维数组
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println(); // 换行
}
}
}