Java Notes

Java 8_Arrays

2018-09-02  本文已影响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);
    }
    
}
上一篇 下一篇

猜你喜欢

热点阅读