Organize the basics of Java--arrays 1

Source: Internet
Author: User

Java Arrays
Arrays are used to store fixed-sizeSimilar typeThe Elements
Statement
Arrays must be declared before they can be used
Datatype[] arrayrefvar;//preferred [] put it in the end for a quick understanding of Java C + +
char[] ch; or char ch[];
char ch = new Char[arraysize];
1. Create a char array containing arraysize elements
2. Assign a reference to the new char array to the variable ch
Use of arrays

public class TestArr{    public static void main(String[] args){        double[] arr = {1.1,2.2,4.4,3.3};        for(double x:arr){            System.out.println(x);                  }//打印所有数组元素        double total = 0;        for(int i = 0;i < arr.length;++i){            total = total + arr[i];                     }        System.out.println("Total = " + total);        //计算并打印所有元素的和        double max = arr[0];        for(int i = 1;i < arr.length;++i ){            if (arr[i] > max) max = arr[i];        }        System.out.println("Max = " + max);    }   //查找并打印最大元素值}输出结果:1.12.24.43.3Total = 11.0Max = 4.4

Array as the parameter of the function:

class A{    static void printArray(int[] array) {        for (int i = 0; i < array.length; i++) {            System.out.print(array[i] + " ");        }    }}public class TestA{  public static void main(String args[]){    int[] arr = {1, 2, 3, 4, 5, 6};    A.printArray(arr);  }}输出结果:1 2 3 4 5 6

Organize the basics of Java--arrays 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.