首页 > 其他分享 >array

array

时间:2022-11-05 17:59:35浏览次数:43  
标签:nums int System println array out

package array;

public class ArrayDemo01 {
    public static void main(String[] args) {
        int[] nums = null;
        nums = new int[5];
        int[] nums2 = new int[3];
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        System.out.println(nums[2]);
        System.out.println(nums2[2]);
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        System.out.println(sum);
    }
}
package array;

public class ArrayDemo02 {
    public static void main(String[] args) {
        int[] nums = {1,3,5,6,9};
        double[] nums3 = {1,3,5,6,9};
        Man[] mans = {new Man(),new Man()};
        System.out.println(nums[3]);
        System.out.println(mans[0]);
        System.out.println(nums);
        System.out.println(nums3);


        int[] nums2 = new int[4];
        int[] a = new int[5];
        a[1] = 2;
        a[3] = 3;
        nums2[0] = 9;
        nums2[1] = 3;
        System.out.println(nums2[0]);
        System.out.println(nums2[1]);
        System.out.println(nums2[2]);
        System.out.println(nums2[3]);
    }
}
package array;

public class Man {
}
package array;

public class ArrayDemo03 {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.print("\n");
        System.out.println("======================================");
        int sum = 0;
        for(int i=0;i<array.length;i++){
            sum += array[i];
        }
        System.out.println("sum = "+sum);
        System.out.println("===========================================");
        int max = array[0];
        for (int i=0;i<array.length;i++){
            if(array[i]>max){
                max = array[i];
            }
        }
        System.out.println(max);
    }



}
package array;

public class ArrayDemo04 {
    public static void main(String[] args) {
        int[] num = {1,3,5,7,9};
        for(int a : num){
            System.out.print(a+" ");
        }
        System.out.println();
        System.out.println("========================");
        double[] a = {1.1,1.5,1.3,1.7,1.9};
        printArray(a);
        System.out.println("========================");
        int[] reverse = reverse(num);
        printArray(reverse);
    }
    public static void printArray(double[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
    public static void printArray(int[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
    public static int[] reverse(int[] array){
        int[] result = new int[array.length];
        for(int i=0,j=result.length-1;i<array.length;i++,j--){
            result[j] = array[i];
        }
        return result;
    }
}
package array;

public class ArrayDemo05 {
    public static void main(String[] args) {
        int[][] nums = {{1,2},{2,3},{3,4},{4,5}};
        int[][] nums1 = {{1,2},{2,3,4},{3,4,5,6},{4,5}};
        printArray(nums[1]);
        printArray(nums1[2]);
        System.out.println(nums[2][0]);
        System.out.println(nums.length);
        System.out.println(nums[1].length);
        System.out.println("========================");
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[i].length; j++) {
                System.out.print(nums[i][j]+" ");
            }
        }
        System.out.println();

        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums1[i].length; j++) {
                System.out.print(nums1[i][j]+" ");
            }
        }
    }
    public static void printArray(int[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
}
package array;

import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayDemo06 {
    public static void main(String[] args) {
        int[] nums = {1,23,5,656,213123,72,123};
        int[] nums1 = {1,23,5,656,213123,72,123};
        System.out.println(nums);//[I@4554617c
        System.out.println(Arrays.toString(nums));
        printArray(nums);
        System.out.println();
        System.out.println("=======================================");

        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        System.out.println("===========================================");

        Arrays.fill(nums,2,5,3);
        System.out.println(Arrays.toString(nums));
        System.out.println("=========================================");

        System.out.println(Arrays.equals(nums,nums1));
        System.out.println("===========================================");

        Arrays.sort(nums1);
        System.out.println(Arrays.binarySearch(nums1,656));
        System.out.println("===========================================");

        int[] a = null;
        a =new int[0];
        System.out.println(Arrays.toString(a));
    }
    public static void printArray(int[] array){
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            if (i == array.length-1){
                System.out.print(array[i]+"]");
            }else {
                System.out.print(array[i]+", ");
            }
        }
    }
}
package array;

import java.util.Arrays;

public class Bubble {
    public static void main(String[] args) {
        int[] nums = {3,1,45,10062,1156,360,15};
        Bubble bubble = new Bubble();
        System.out.println(Arrays.toString(bubble.sort(nums)));
    }
    public int[] sort(int[] array){
        int temp = 0;
        for (int i = 0; i < array.length-1 ; i++) {
            int flag = 0;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j]<array[j+1]){
                    temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                    flag = 1;
                }
            }
            if (flag == 0){
                break;
            }
        }
        return array;
    }
}
package array;

import java.util.Arrays;

public class SelectSort {
    public static void main(String[] args) {
        int[] nums = {3,1,45,10062,1156,360,15};
        SelectSort sort = new SelectSort();
        int[] a = sort.sort(nums);
        System.out.println(Arrays.toString(a));
    }
    public int[] sort(int[] array){
        int temp = 0;
        for (int i = 0; i < array.length-1; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[minIndex]>array[j]){
                    minIndex = j;
                }
            }
            if (i != minIndex){
                temp = array[i];
                array[i] = array[minIndex];
                array[minIndex] = temp;
            }
        }
        return array;
    }
}
package array;

public class ArrayDemo07 {
    public static void main(String[] args) {
        int[][] array1 = new int[11][11];
        array1[1][2] = 1;
        array1[2][3] = 2;
        array1[4][5] = 1;
        System.out.println("原始数列:");
        for (int[] nums1 : array1) {
            for (int nums2 : nums1) {
                System.out.print(nums2+"\t");
            }
            System.out.println();
        }
        System.out.println("=============================================");
        int sum = 0;
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if (array1[i][j] != 0){
                    sum ++;
                }
            }
        }
        int[][] array2 = new int[sum+1][3];
        array2[0][0] = array1.length;
        array2[0][1] = array1[0].length;
        array2[0][2] = sum;
        int count = 0;
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if (array1[i][j] != 0){
                    count ++;
                    array2[count][0] = i;
                    array2[count][1] = j;
                    array2[count][2] = array1[i][j];
                }
            }
        }
        System.out.println("转换为稀疏数组:");
        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                System.out.print(array2[i][j]+"\t");
            }
            System.out.println();
        }
        System.out.println("===============================================");
        int[][] array3 = new int[array2[0][0]][array2[0][1]];
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }
        System.out.println("还原:");
        for (int[] ints : array3) {
            for (int anInt : ints){
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
    }
}

时间复杂度怎么算的 八大排序算法

标签:nums,int,System,println,array,out
From: https://www.cnblogs.com/799rijiyuelei/p/16860694.html

相关文章