Dsa Using Java 简明教程

DSA using Java - Arrays

Array Basics

数组是一个容器,可以容纳固定数量的项,并且这些项应为同类型。大多数数据结构使用数组来实现其算法。以下是理解数组概念的重要术语

Array is a container which can hold fix number of items and these items should be of same type. Most of the datastructure make use of array to implement their algorithms. Following are important terms to understand the concepts of Array

  1. Element − Each item stored in an array is called an element.

  2. Index − Each location of an element in an array has a numerical index which is used to identify the element.

Array Representation

array

根据以上所示的说明,以下是要考虑的重要要点。

As per above shown illustration, following are the important points to be considered.

  1. Index starts with 0.

  2. Array length is 8 which means it can store 8 elements.

  3. Each element can be accessed via its index. For example, we can fetch element at index 6 as 9.

Basic Operations

数组支持的基本操作如下。

Following are the basic operations supported by an array.

  1. Insertion − add an element at given index.

  2. Deletion − delete an element at given index.

  3. Search − search an element using given index or by value.

  4. Update − update an element at given index.

在 java 中,当使用大小初始化一个数组时,它会按以下顺序为其元素分配默认值。

In java, when an array is initialized with size, then it assigns defaults values to its elements in following order.

Data Type

Default Value

byte

0

short

0

int

0

long

0L

float

0.0f

double

0.0d

char

'\u0000'

boolean

false

Object

null

Demo

package com.tutorialspoint.array;

public class ArrayDemo {
   public static void main(String[] args){

      // Declare an array
      int intArray[];

      // Initialize an array of 8 int
      // set aside memory of 8 int
      intArray = new int[8];

      System.out.println("Array before adding data.");

      // Display elements of an array.
      display(intArray);

      // Operation : Insertion
      // Add elements in the array
      for(int i = 0; i< intArray.length; i++)
      {
         // place value of i at index i.
         System.out.println("Adding "+i+" at index "+i);
         intArray[i] = i;
      }
      System.out.println();

      System.out.println("Array after adding data.");
      display(intArray);

      // Operation : Insertion
      // Element at any location can be updated directly
      int index = 5;
      intArray[index] = 10;

      System.out.println("Array after updating element at index " + index);
      display(intArray);

      // Operation : Search using index
      // Search an element using index.
      System.out.println("Data at index " + index + ": "+ intArray[index]);

      // Operation : Search using value
      // Search an element using value.
      int value = 4;
      for(int i = 0; i< intArray.length; i++)
      {
         if(intArray[i] == value ){
            System.out.println(value + " Found at index "+i);
            break;
         }
      }
      System.out.println("Data at index " + index + ": "+ intArray[index]);
   }

   private static void display(int[] intArray){
      System.out.print("Array : [");
      for(int i = 0; i< intArray.length; i++)
      {
         // display value of element at index i.
         System.out.print(" "+intArray[i]);
      }
      System.out.println(" ]");
      System.out.println();
   }
}

如果我们编译并运行上述程序,它将生成以下结果 -

If we compile and run the above program then it would produce following result −

Array before adding data.
Array : [ 0 0 0 0 0 0 0 0 ]

Adding 0 at index 0
Adding 1 at index 1
Adding 2 at index 2
Adding 3 at index 3
Adding 4 at index 4
Adding 5 at index 5
Adding 6 at index 6
Adding 7 at index 7

Array after adding data.
Array : [ 0 1 2 3 4 5 6 7 ]

Array after updating element at index 5
Array : [ 0 1 2 3 4 10 6 7 ]

Data at index 5: 10
4 Found at index: 4