javase(4)_数组
一、数组概述
数组可以看成是多个相同类型数据组合,对这些数据的统一管理.
数组变量属于引用类型,数组也可以看成对象,数组中的每个元素相当于该对象的成员变量.
数组中的元素可以是任意类型,包括基本类型和引用类型,有默认初始值(按照成员变量的规则).
二、一维数组定义
实例:
int[] a= new int[]; //对
int[] b= new int[]{,,}; //对
int[] c={,,}; //对
int[] d=new int[]{,}; //错 int[] a=new int[];
int b=a[]; //对,有默认值
int b=a[]; //运行期错误,编译无问题。 int[]a;
int k = a[]; //编译报错,未初始化,只是开辟了栈内存 Person[] p=new Person[]; p[].age; //运行期空指针异常
三、数组长度
java中每个数组都有一个public final int 的length属性,表示数组的长度,一旦确定就不能改变大小.
四、二维数组定义
二维数组可以看成数组的数组.
实例
int a[][]={{,},{,},{,}} //对
int a[][]=new a[][] //二维变长数组
a[]=new int[];
a[]=new int[];
a[]={,}; //报错,此处和一维数组有区别
int a[][]=new a[][]; //错
五、数组实例
/*取最值*/
int[] array = new int[]{,,,,,};
int max=;
for(int i=;i<array.length;i++){
if(array[max]<array[i]){
max=i;
}
}
System.out.println(array[max]); /*选择排序*/
for(int i=;i<array.length-;i++){
int min=i;
for(int j=i+;j<array.length;j++){
if(array[min]>array[j]){
min=j;
}
}
if(min != i){
array[i] = array[i]^array[min];
array[min] = array[i]^array[min];
array[i] = array[i]^array[min];
}
} /*冒泡排序法*/
for(int i=;i<array.length-;i++){
for(int j=;j<array.length-i-;j++){
if(array[j]>array[j+]){
array[j]=array[j]^array[j+];
array[j+]=array[j]^array[j+];
array[j]=array[j]^array[j+];
}
}
} /*反转数组*/
public static void reverseArray(int[] arr){
for(int start=,end=arr.length-; start<end ; start++,end--){
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
} /*二分查找法*/
public static int getArrayIndex(int target){
int num=;
int left=;
int last=array.length-;
int middle = (left+last)>>;
while(array[middle]!=target){
if(array[middle]>target){
last=middle-;
}else{
left=middle+;
}
middle=(left+last)>>;
num++;
}
return middle;
}
ps:面试题,插入一个数,保持数组排序,插入的位置?
二分查找条件改为:while(array[left]<=array[last]),插入的位置:left+1
六、实际使用数组时,使用java.util.Arrays类和apache.long.ArrayUtils类中提供的数组方法。
ps(常用的数组方法): http://www.iteye.com/news/28296
javase(4)_数组的更多相关文章
- 选择排序_C语言_数组
选择排序_C语言_数组 #include <stdio.h> void select_sort(int *); int main(int argc, const char * argv[] ...
- 插入排序_C语言_数组
插入排序_C语言_数组 #include <stdio.h> void insertSort(int *); int main(int argc, const char * argv[]) ...
- 快速排序_C语言_数组
快速排序_C语言_数组 #include <stdio.h> void quickSort(int *, int, int); int searchPos(int *, int, int) ...
- 冒泡排序_C语言_数组
冒泡排序_C语言_数组 #include <stdio.h> //冒泡排序 小->大 void sort(int * pArray, int len); int main(int a ...
- Net基础篇_学习笔记_第九天_数组_冒泡排序(面试常见题目)
冒泡排序: 将一个数组中的元素按照从大到小或从小到大的顺序进行排列. for循环的嵌套---专项课题 int[] nums={9,8,7,6,5,4,3,2,1,0}; 0 1 2 3 4 5 6 7 ...
- Net基础篇_学习笔记_第九天_数组_三个练习
练习一: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- Net基础篇_学习笔记_第九天_数组
结构:一次性存储不同类型的变量: 数组:一次性存储相同类型的变量: 数组的语法: 数组类型[ ] 数组名=new 数组类型[数组长度]: int[ ] nums=new int[10]; 数组初值都是 ...
- C++_系列自学课程_第_7_课_数组_《C++ Primer 第四版》
说到数组,大家应该都很熟悉,在C.Pascal.Java等语言中,都有数组的概念.在C++中也提供了对数组的支持.数组简单来说就是一堆相同 数据类型对象的集合. 这里要把握住两个要点: 相同的数据类型 ...
- 大数计算_BigNum优化_加减乘除乘方取余_带注释_数组
#include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...
随机推荐
- Unity2016 Unity3D开发VR游戏的经验
http://z.youxiputao.com/articles/8313 在4月12日的Unite 2016大会上,暴风魔镜高级产品经理吴涛分享他用Unity3D开发VR游戏的经验,以下为分享实录: ...
- javaScript中for-in语句
for-in语句是一种精准的迭代语句,用来枚举对象的属性 实例: <!DOCTYPE html><html><head> <title>For-In S ...
- mysql之SQL入门与提升(三)
今天继续讲mysql数据库 先创建一张Persons表 CREATE TABLE Persons(P_Id int NOT NULL,LastName varchar(255) NOT NULL,Fi ...
- react native 安卓打包--mac环境,如果打包不成功可注意下my-release-key.keystore的位置关系(绝对路径)
// my-release-key.keystore和my-key-alias都是可修改的名称 1.生成签名密钥(keytool -genkey -v -keystore my-release-key ...
- eclipse添加tomcat运行环境
- 字符串匹配,KMP算法
KMP的详解见:https://segmentfault.com/a/1190000008575379 主要难点在于Next数组的理解,KMP是不需要回溯的匹配算法. #include<iost ...
- python入门之实例-购买商品
需求: 选择商品,结算所选的商品 #目前总资产 asset_all = 0 #所选商品总价 all_price = 0 #购物车列表,目前已选择商品 #每个元素的结构:"商品名": ...
- TDH-kafka
一.kafka原理:1.broker:一个kafka实例,多个broker组成kafka集群: 2.topic:逻辑概念,同一类数据的集合,可以看做一张表: 3.producer:将数据写入topic ...
- 通用全局CSS样式
PC全局样式 *{padding:0;margin:0;} div,dl,dt,dd,form,h1,h2,h3,h4,h5,h6,img,ol,ul,li,table,th,td,p,span,a{ ...
- Android仿ios底部弹出框效果
准备: public class ActionSheet { public interface OnActionSheetSelected { void onClick(int whichButton ...