知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

      • Theory:Scanning the input
      • Theory:Arithmetic operations
      • Theory:Integer types and operations
      • Theory:Increment and decrement
      • Theory:Relational operators
      • Theory:Ternary operator
      • Theory:The while and do-while loops
      • Theory:Branching statements
      • Theory:Characters
      • Theory:String
      • Theory:Boolean and logical operators
      • Theory:Sizes and ranges
      • Theory:Switch statement
      • Theory:Declaring a method
      • Theory:The main method
      • Theory:Type casting
      • Theory:Primitive and reference types
      • Theory:Array
        • Create an array
          • Declaration
          • Instantiation
          • Initialization
          • General write style
        • To obtain the length of the array
        • Accessing elements
        • The utility class Arrays
      • Theory:Arrays as parameters
      • Theory:Iterating over arrays
      • Theory:Multidimensional array
      • Theory:Final variables
    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java basic
Jim
2022-04-21
目录

Theory:Array

# Theory:Array

# Create an array

# Declaration

int[] numbers;

// int numbers[];    less used in parctice
1
2
3

Declare the array variable's type and name. We must use two special characters [] after the type or the variable's name.

# Instantiation

int n = ...;    // n is a length of an array
int[] number;    // declaration

number = new int[n];    // instantiation
1
2
3
4

The new keyword represents instantiating the object. It depends on the array's type and length.

提示

The size of an array cannot be greater than Integer.MAX_VALUE. Actually, it is even slightly smaller than this value.

注意

Instantiation happens when memory is allocated for this object.

If you don't assign some values for the array, will use the default values of its type.

For example, the int type's default value is 0(zero) and the String type's default value is null.

So, at instantiation phases, exists implicit initialize action.

# Initialization

int n = ...;    // n is a length of an array
int[] number;    // declaration
number = new int[n];    // instantiation

number = { 1, 2, 3, 4 };    // initialization
1
2
3
4
5

# General write style

Yeah, we can separate declaration, instantiation, and initialization like above. Also, we can join them and put in fewer lines:

int[] number = new int[4];
number = { 1, 2, 3, 4 };
1
2

Or this:

int[] number = new int[] { 1, 2, 3, 4 };
1

Or even like this:

int[] numbers = { 1, 2, 3, 4 };
1

# To obtain the length of the array

int[] array = { 1, 2, 3, 4 }; // an array of numbers

int length = array.length; // number of elements of the array

System.out.println(length); // 4
1
2
3
4
5

# Accessing elements

Set the value by the index:

array[index] = val;
1

Get the value by the index:

val = array[index];
1

Indexes of an array have numbers from 0 to length – 1 inclusive.

Let's see an example.

int[] numbers = new int[3]; // numbers: [0, 0, 0]
numbers[0] = 1; // numbers: [1, 0, 0]
numbers[1] = 2; // numbers: [1, 2, 0]
numbers[2] = numbers[0] + numbers[1]; // numbers: [1, 2, 3]
1
2
3
4

提示

If we try to access a non-existing element by an index then a runtime exception occurs.

ArrayIndexOutOfBoundsException

# The utility class Arrays

If you need to process arrays, you can import and use standard methods grouped in the utility class Arrays.

  • convert an array to string using Arrays.toString(array) and then print it:
byte[] famousNumbers = { 0, 1, 2, 4, 8, 16, 32, 64 };
String arrayAsString = Arrays.toString(famousNumbers); // [0, 1, 2, 4, 8, 16, 32, 64]
System.out.println(arrayAsString);
1
2
3
  • sorting a whole array or a part of it using Arrays.sort(array):
long[] bigNumbers = { 200000000L, 400000000L, 100000000L, 300000000L }; // it's unsorted

Arrays.sort(bigNumbers); // sorting whole array

System.out.println(Arrays.toString(bigNumbers)); // [100000000, 200000000, 300000000, 400000000]
1
2
3
4
5
  • comparing arrays: two arrays are equal if they contain the same elements in the same order:
int[] numbers1 = { 1, 2, 5, 8 };
int[] numbers2 = { 1, 2, 5 };
int[] numbers3 = { 1, 2, 5, 8 };

System.out.println(Arrays.equals(numbers1, numbers2)); // it prints "false"
System.out.println(Arrays.equals(numbers1, numbers3)); // it prints "true"
1
2
3
4
5
6
  • filling a whole array or a part of it by some values:
int size = 10;
char[] characters = new char[size];

// It takes an array, start index, end index (exclusive) and the value for filling the array
Arrays.fill(characters, 0, size / 2, 'A'); 
Arrays.fill(characters, size / 2, size, 'B');

System.out.println(Arrays.toString(characters)); // it prints [A, A, A, A, A, B, B, B, B, B]
1
2
3
4
5
6
7
8

Of course, the Arrays class contains a lot of other useful methods, including array copying, searching in arrays, and so on. For details see here (opens new window).

编辑 (opens new window)
#Java#Array
上次更新: 2022/09/26, 16:55:15
Theory:Primitive and reference types
Theory:Arrays as parameters

← Theory:Primitive and reference types Theory:Arrays as parameters→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式