Arrays in Java--数组

In this session, we discuss arrays in data structures.

本节我们讨论数组。

Then we look at two search algorithms and a sort algorithm.

然后讨论两种查找算法和一种排序算法。

Array Definition

数组的定义

An array is a sequenced collection of variables all of the same type.

数组是一些具有相同数据类型的变量的有序集合。

Each variable, or cell, in an array has an index, which uniquely refers to the value stored in that cell.

数组中的每一个变量(也叫细胞)都有其独自的索引(下标),索引指向的是这个元素里面存储的值。

The cells of an array are numbered 0,1,2, and so on.

元素的下标从0开始0,1,2这样依次排列。

Each value stored in an array is often called an element of that array.

存储在数组里面的每一个值都叫做这个数组的一个元素。

So, once an array is created, its size is fixed.

所以,一旦我们创建一个数组,其长度就固定了。

An array reference variable is used to access the elements in an array using an index.

数组这种引用类型变量就是用下标来访问其中元素的。

Java and most other high-level languages provide a data structure, the array, which stores a fixed-size sequential collection of elements of the same type.

包括Java在内的大多数高级程序设计语言都提供数组这种数据结构来存储定长的,有序的同种类型元素。

Searching Arrays

数组的查找

Searching is the process of looking for specific element in an array: for example, discovering whether a certain score is included in a listed of scores.

查找是在一个数组中寻找某个特定值的过程,比如说找一个特定的分数在不在一个分数表中就是一种查找。

Searching is a common task in computer programming.

查找是编程中很常见的任务。

There are many algorithms and data structures devoted to searching.

有很多关于查找的算法和数据结构。

In this session, 2 commonly used approaches are discussed: linear search and binary search.

本节我们将讨论两种常用的查找算法:线性查找和二分查找。

The binarySearch method returns the index of the element in the list that matches the search key if it is contained in the list.

binary这个方法返回的是给定的查找的值在数组中的下标(如果这个值在数组中存在的话)。

Otherwise, it returns -insertion point-1.

如果数组中没有要查找的值,那就返回最后一次查找的下标-1。

The insertion point is the point at which the key would be inserted into the list.

插入点就是这个值能够插入的地方。

The array.binarysearch

Since binary search is frequently used in programming, Java provides several overloaded binarySearch methods for searching a key in an array of int, double, char, short, long, and float in the Java.util.Arrays class.

因为二分查找很常用,Java提供了二分查找算法在库里面。可以直接调用

Sorting Arrays

数组的排序

sorting, like searching, is also a common task in computer

Since binary search is frequently used in programming, Java provides several overloaded binarySearch methods for searching a key in an array of int, double, char, short, long, and float in the Java.util.Arrays class.

因为排序很常用,Java提供了排序算法在库里面。可以直接调用

这里贴一张API文档的截图

Arrays of Characters or Object References

由字符或对象组成的数组

An array can store primitive elements, such as characters.

数组可以存储一组字符,比如下面这个。

An array can also store references to objects.

也可以存储指向对象的一组引用类型,比如下面这个Java Example: Game Entries

举例:游戏记录

A game entry stores the name of a player and the best score so far in a game

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只萌新兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值