Java 8_Arrays

作者: 綿綿_ | 来源:发表于2018-09-02 23:19 被阅读0次

Array Rules
An array can contain only values of the same type
An arrays size can't be changed once it is set
An array is an object

Create an array

You create an array with dataType[] arrayName = new dataType[sizeOfArray];

import java.util.Arrays;

public class LessonNine {
    
    public static void main(String[] args){
    
        int[] randomArray; 

        int[] numberArray = new int[10];
        
        // Most create array and define size first
        randomArray = new int[20]; 
        randomArray[1] = 2;
        
        // You can also create the array and its values from the start
        String[] stringArray = {"Just", "Random", "Words"};

You can add values with a loop
arrayName.length returns the number of elements in the array

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

Draws 41 lines on the screen

        int k = 1;
        while(k <= 41){ System.out.print('-'); k++; }
        System.out.println();

Cycles through all of the boxes in the array and prints them

        for(int j = 0; j < numberArray.length; j++)
        {
            System.out.print("| " + j + " ");
        }
        System.out.println("|");

Multidimensional Array

        String[][] multiDArray = new String[10][10];
        
        // Adding values to a multidimensional array
        
        for(int i = 0; i < multiDArray.length; i++)
        {
            
            // To get the length for the array in the array you must follow it 
            // with brackets with the index between them like [i]
            
            for(int j = 0; j < multiDArray[i].length; j++)
            {
                
                multiDArray[i][j] = i + " " + j;
                
            }
            
        }

Prints out a multidimensional array with the values being the indexes

        for(int i = 0; i < multiDArray.length; i++)
        {
            
            for(int j = 0; j < multiDArray[i].length; j++)
            {
                
                System.out.print("| " + multiDArray[i][j] + " ");
                
            }
            System.out.println("|");
            
        }
        

You can use the enhanced for loop to print out array values
for(itemDataType tempVariable : arrayName)

        for(int row : numberArray)
        {
            System.out.print(row);
        }
        System.out.println("\n");
        
        // To use enhanced for a multidimensional array you follow this formula
        // for(dataType[] varForRow : arrayName)
        
        for(String[] rows : multiDArray) 
        {
            // for(elementDataType varForColumn : varForRow)
            for(String column : rows) 
            {
                System.out.print("| " + column + " ");
            }
                System.out.println("|");
        }
        

Ways to copy an array

copy from the beginning to the index that you pointed

        int[] numberCopy = Arrays.copyOf(numberArray, 5);
        for(int num : numberCopy)
        {
            System.out.print(num);
        }
        System.out.println("\n");
        

copy of range

         int[] numberCopy = Arrays.copyOf(numberArray, 1, 5);

Print out the whole array with toString

        System.out.println(Arrays.toString(numberCopy));

Do define a default value for an array use fill

      //Arrays.fill(arrayName, valueToFill);
    // valueToFill must be the same for each element in the array
        int[] moreNumbers = new int[100];
        Arrays.fill(moreNumbers, 2);

Filling a multidimensional array

        char[][] boardGame = new char[10][10];
        for(char[] row : boardGame)
        {
            Arrays.fill(row, '*');
        }

sort an array using sort()

        int[] numsToSort = new int[10];
        
        // Generate array full of random numbers
        for(int i = 0; i < 10; i++)
        {
            numsToSort[i] = (int) (Math.random() * 100);
        }
        
        // Sort the array in ascending order
        Arrays.sort(numsToSort);
        
        System.out.println(Arrays.toString(numsToSort));

BinarySearch in array

Binary search returns the index for the searched for value,If it doesn't find it it returns a negative number

        int whereIs50 = Arrays.binarySearch(numsToSort, 50);
        
        System.out.println(whereIs50);
    }
    
}

相关文章

  • Java 8_Arrays

    Array RulesAn array can contain only values of the same t...

  • Java(JavaEE)学习线路图1

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • Java学习线路图

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 大数据学习线路图

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 大数据学习教程

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 一篇文章,全面解读Android面试知识点

    Java Java基础 Java集合框架 Java集合——ArrayList Java集合——LinkedList...

  • java学习路线

    javaSE java基础语法 java文件操作 java网络操作 java多线程 java数据库操作 java ...

  • java编程分类

    一、java简介java开发环境配置java基础语法java对象与类java基本数据类型java变量类型java修...

  • Java-01初识Java

    Java的3大版本 Java有三大版本,Java SE, Java ME, Java EE Java SE(Jav...

  • JAVA循环语句(四)9.29

    Java条件 if Java条件if...else Java条件多重if Java条件语句 嵌套if Java条件...

网友评论

    本文标题:Java 8_Arrays

    本文链接:https://www.haomeiwen.com/subject/qcamwftx.html