Table of Contents
Sum of elements in an array
Use a two-dimensional array to print a 10-line Yang Hui triangle
Find the maximum, minimum, average, and Sum, etc.
*Use a simple array
Linear search
This is a matrix programming implementation question. Matrices in Java are generally implemented through two-dimensional arrays.
Home Java javaTutorial 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

Apr 19, 2023 pm 10:40 PM
java

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
Copy after login

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();
        }
    }
}
Copy after login

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);
    }
}
Copy after login

*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");
        }
    }
}
Copy after login

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];
        }
Copy after login

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;
        }
Copy after login
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");
        }
    }
}
Copy after login

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("很遗憾没有找到");
        }
    }
}
Copy after login

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]);
        }
    }
}
Copy after login

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);
    }
}
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles