search
HomeJavajavaTutorialJava uses a two-dimensional array to print a 10-line Yang Hui triangle

Sum of elements in an array

public class T02 {
    public static void main(String[] args) {
        int[][]arr=new int[][]{{1,2,3,4,5},{1,2,3,5},{8,9,7}};
        int sum=0;
        for(int i=0;i< arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
               sum=arr[i][j]+sum;
            }
        }
        System.out.println("sum="+sum);
    }
}
//和为50

Use a two-dimensional array to print a 10-line Yang Hui triangle

public class T02 {
    public static void main(String[] args) {
        //声明并且初始化数组
        int[][]arr=new int[10][];
        //给数组的元素赋值
        for(int i=0;i< arr.length;i++){
            arr[i]=new int[i+1];
            arr[i][0]=arr[i][i]=1;
            if(i>1){
                for(int k=1;k<arr[i].length-1;k++){
                    arr[i][k]=arr[i-1][k-1]+arr[i-1][k];
                }
            }
        }
        //遍历数组
        for(int i=0;i< arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
    }
}

Find the maximum, minimum, average, and Sum, etc.

public class T03 {
    public static void main(String[] args) {
        int[] arr=new int[10];
        for(int i=0;i< arr.length;i++){
            arr[i]=(int)Math.random()*((99-10+1)+10);
            //[a,b]中的随机数的公式:Math.readom()*((b-a+1)+a).
            //注意这里出来的为double类型。
        }
        //求最大值
        int maximum=0;
        for(int i=0;i< arr.length;i++){
            if(maximum<arr[i]){
                maximum=arr[i];
            }
        }
        System.out.println("最大值为:"+maximum);
        //求最小值
        int minimum=arr[0];
        for(int i=1;i< arr.length;i++){
            if(minimum>arr[i]){
                minimum=arr[i];
            }
        }
        System.out.println("最大值为:"+minimum);
        //求和
        int sum=0;
        for(int i=1;i< arr.length;i++){
            sum=sum+arr[i];
        }
        System.out.println("sum:"+sum);
        //求平均数
        System.out.println("平均数为:"+sum/ arr.length);
    }
}

*Use a simple array

(1)Create a class named T04 and declare two variables array1 and array2 in the main() method

They It is an array of type int[].

(2) Use curly brackets {} to initialize array1 to 8 prime numbers: 2, 3, 5, 7, 11, 13, 17, 19.

(3) Display the contents of array1.

(4) Assign array2 variable equal to array1, modify the even index element in array2 to make it equal to the index value (such as array[0]=0, array[2]=2). Print out array1. **Thinking: What is the relationship between array1 and array2?
Extension: Modify the question to realize the copying of array1 array by array2

public class T04 {
    public static void main(String[] args) {
        int[] array1,array2;
        array1=new int[]{2,3,5,7,11,13,17,19};
        for(int i=0;i< array1.length;i++){
            System.out.print(array1[i]+"\t");
        }     //赋值array1变量等于array2     //不能称作数组的复制
        array2=array1;
        for(int i=0;i< array1.length;i++){
            if(i%2==0){
                array2[i]=i;
            }
        }
        System.out.println();
        System.out.println("******************************************");
        for(int i=0;i< array1.length;i++){
            System.out.print(array1[i]+"\t");
        }
    }
}

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

(1) The addresses of array1 and array2 The values ​​are the same, pointing to the only array entity in the heap space

(2)

 for(int i=0;i< array1.length;i++){
            array2[i]=array1[i];
        }

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

Method 2

int i=0;
        int j=0;
        for(i=0,j= arr.length-1;i<j;i++,j--){
            int a=arr[i];
            arr[i]=arr[j];
            arr[j]=a;
        }
public class T05 {
    public static void main(String[] args) {
        int[]arr=new int[]{1,2,3,4,5,6,7,8,9};
        for(int i=0;i< arr.length;i++){
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
        for(int i=0;i< arr.length;i++){
            if(i< arr.length-1-i){
                int a=arr[i];
                arr[i]=arr[arr.length-1-i];
                arr[arr.length-1-i]=a;
            }
        }
        for(int i=0;i< arr.length;i++){
            System.out.print(arr[i]+"\t");
        }
    }
}

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

##Binary search

//The premise must be in order

Example : Let’s first look at

import java.util.Scanner;

public class T07 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int a= s.nextInt();
        int[]arr=new int[]{1,2,3,4,6,7,8,9,10};
        int left=0;
        int right= arr.length-1;
        boolean is=true;
        while(left<=right){
            int average=(int)(left+right)/2;
            if(arr[average]>a){
                right=average-1;
            } else if(a==arr[average]){
                System.out.println("找到了,下标是:"+average);
                is=false;
            } else {
                left = average + 1;
//            }if(left==right){
//                System.out.println("没有找到");
//                is=false;
            }
        }
        if(is){
            System.out.println("很遗憾没有找到");
        }
    }
}

bubble sorting from small to large

public class T08 {
    public static void main(String[] args) {
        int[]arr=new int[]{33,55,2,6,-8,-5,66,1,63};
        for(int i=0;i< arr.length-1;i++){
            for(int j=0;j< arr.length-1-i;j++){
                if(arr[j]>arr[j+1]){
                    int a=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=a;
                }
            }
        }
        for(int i=0;i< arr.length;i++) {
                System.out.println(arr[i]);
        }
    }
}

Java uses a two-dimensional array to print a 10-line Yang Hui triangle## Find the sum of the diagonal elements of a 3*3 matrix

This is a matrix programming implementation question. Matrices in Java are generally implemented through two-dimensional arrays.

The specific code is as follows:

import java.util.Random;

/**
 * 求一个3*3矩阵对角线元素之和
 *
 * @author ChenZX
 *
 */
public class Test04 {

    public static void main(String[] args) {
        int sum = 0; //和
        int[][] arr = new int[3][3];
        Random r = new Random();
        for(int i=0;i<3;i++){    //随机生成矩阵
            for(int j=0;j<3;j++){
                arr[i][j] = r.nextInt(10);  //0到9
            }
        }
        for(int i=0;i<3;i++){      //遍历矩阵
            for(int j=0;j<3;j++){
                System.out.print(arr[i][j]+" ");   //打印矩阵元素
                if(i==j){   //如果为对角线元素
                    sum += arr[i][j];  //求和
                }
            }
            System.out.println(); //每输出3个元素换行
        }
        System.out.println("此矩阵对角线的和为:"+sum);
    }
}

The above is the detailed content of Java uses a two-dimensional array to print a 10-line Yang Hui triangle. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment