import java.util.Arrays; /** * Created with IntelliJ IDEA. * Description: * User: 出其东门 * Date: 2022-07-20 * TIME: 10:11 */ class Person{ //字段-》属性 -》成员变量-》类的内部 方法的外部的-》1.普通成员变量 静态成员变量 public String name = "j"; public int age = 18; //方法-》行为 public void eat() { System.out.println(name+"正在吃饭"); } public void sleep() { System.out.println(name+"正在睡觉"); } } public class TestDemo2 { public static void main(String[] args) { Person person = new Person(); //普通成员变量的访问需要通过对象的引用过来访问的 System.out.println(person.name); } public static void main8(String[] args) { //Person person = null; Person person = new Person(); Person person2 = new Person(); Person person3 = new Person(); Person person4 = new Person(); Person person5 = new Person(); //int a = 10; } public static boolean isUp(int[] array) { for (int i = 0; i < array.length-1; i++) { if (array[i] > array[i+1]) { return false; } } return true; } //冒泡排序 public static void bubbleSort(int[] array) { //i表示趟数 for (int i = 0; i < array.length-1; i++) { boolean flg = false; for (int j = 0; j < array.length-1-i; j++) { if(array[j] > array[j+1]) { int tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; flg = true; } }if (flg == false) { return; } } } public static void main1(String[] args) { int[] array = {11,2,13,4,15}; bubbleSort(array); System.out.println(Arrays.toString(array)); } //填充 public static void main2(String[] args) { int[] array = new int[10]; Arrays.fill(array,2,6,1999);// [2,6) System.out.println(Arrays.toString(array)); } //数组的逆置 public static void reverse(int[] array) { int i = 0; int j= array.length-1; while (i < j) { int tmp = array[i]; array[i] = array[j]; array[j] = tmp; i++; j--; } } public static void main3(String[] args) { int[] array = {1,2,3,4,5,6}; reverse(array); System.out.println(Arrays.toString(array)); } public static void func(int[] array) { int i = 0; int j = array.length - 1; while (i < j) { while (i < j && array[i] % 2 == 0) { i++; }//i下标一定是奇数 while (i < j && array[j] % 2 != 0) { j--; }//j遇到了偶数 int tmp = array[i]; array[i] = array[j]; array[j] = tmp; } } public static void main4(String[] args) { int[] array = {1, 2, 3, 4, 5, 6}; func(array); System.out.println(Arrays.toString(array)); } public static int[] copyArray(int[] array) { int[] copy = new int[array.length]; for (int i = 0; i < array.length; i++) { copy[i] = array[i]; } return copy; } public static void main5(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = array.clone(); System.out.println(Arrays.toString(copy)); /* int[] copy = new int[array.length]; System.arraycopy(array,0,copy,0,array.length); System.out.println(Arrays.toString(copy));*/ //int[] ret = Arrays.copyOf(array,array.length*2); //int[] ret = Arrays.copyOfRange(array,1,3);//[1,3) //System.out.println(Arrays.toString(ret)); //int[] ret = copyArray(array); //System.out.println(Arrays.toString(ret)); } public static void main6(String[] args) { int[][] array = {{1,2,3},{4,5,6}}; int[][] array2 = new int[][] {{1,2,3},{4,5,6}}; int[][] array3 = new int[2][3]; } //二维数组的基本打印方式 public static void main7(String[] args) { int[][] array = {{1,2,3},{4,5,6}}; /*System.out.println(array.length); System.out.println(array[0].length);*/ for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]+" "); } System.out.println(); } System.out.println("================"); for (int[] ret : array) { for (int x : ret) { System.out.print(x +" "); } System.out.println(); } System.out.println("================"); System.out.println(Arrays.deepToString(array)); } }
JAVA 类和对象基础知识
最新推荐文章于 2024-09-02 22:33:56 发布