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 ...
随机推荐
- caller和callee的解析与使用-型参与实参的访问
caller:是一个函数引用(当前执行函数”被调用的地方”{即这个”被调用的地方”函数引用},如果这个”被调用的地方”是window,则返回[null]),是函数名的属性: var a = funct ...
- 2013 Noip提高组 Day2
3288积木大赛 正文 题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前 ...
- Withdraw From OI
Withdraw From OI 已经退役一周了,但还是迟迟没有去写退役记,在这个2017年的最后一天,写下这一篇沉重的“withdraw from OI”. 距离联赛成绩出来已经一个多月了.这一个月 ...
- 怎么解决UIScrollView把uitableviewcell的点击事件屏蔽了
[self.contentView addSubview:self.scrollView]; self.scrollView.userInteractionEnabled = NO; [self.co ...
- assembly x86(nasm)修改后的日常
data segment ENG db 'SUNdayS Coming I Wanna Drive My Car,SUN,SUN$' ;9,3 sun1 db 'SUN' swcount db 0ah ...
- A - Wireless Network
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> ...
- Gym - 101810F ACM International Collegiate Programming Contest (2018)
bryce1010模板 http://codeforces.com/gym/101810 #include<bits/stdc++.h> using namespace std; #def ...
- ubuntu管理apt包的常用命令
安装 apt-get install nginx #安装 apt-get install nginx --reinstall #重新安装 删除 apt-get remove nginx #卸载 apt ...
- arcgis mdb 数据中的shp 如何合并一起
如上操作 一直往下就可以啦 选择数据源 就可以了,然后就可以load 其他数据啦 ,坐标系要一直
- js对象引用赋值后
a={f:1} b={} b.a=a console.log(b.a) a.b=2 console.log(b.a) a={f:1} b={} b.a=a console.log(b.a) a={b: ...