java学习——数组
元素类型[] 数组名 = new 元素类型[元素个数或数组长度];
array 为引用数据类型|-数组数据类型
|
内存结构:程序在运行时,需要在内存中的分配空间。为了提高运行的效率,
有对空间进行不同区域的划分,因为每一片区域都有特定的处理数据方式和
内存内存管理方式。
栈内存:用于存储局部变量,而数据使用完,所占的空间会自动释放。
堆内存:1,数组和对象,通过new建立的实例都存放在堆内存中
2,每一个实体都有内存地址值。
3,实体中的变量都有默认初始值。
4,实体不在被使用,会在不确定的时间被垃圾回收器回收。
在堆内存中默认初始化值:int 为 0, double 为 0.0 , boolean 为 false
方法区
本地方法区
寄存器
垃圾回收机制[自动执行]
数组在内存中的存储方式:

数组排序:
选择排序:内循环结束一次,最值出现在头角标位置上。
冒泡排序:相邻的两个元素进行比较。如果符合条件换位。
public class ArraySortDemo
{
private static int[] arr = {1, 3, 2, 4, 13, 9, 6};
public static void main(String[] args)
{
//System.out.println("Hello World!");
//selectSort(arr);
bubbleSort(arr);
ArrayPrint(arr);
//Arrys.sort(arr); java开发中用这个函数进行排序。
} public static void selectSort(int[] arr) { for (int x = 0; x<arr.length - 1 ; x++ )
{
for (int y = x + 1; y<arr.length ;y++ )
{
if (arr[x] > arr[y])
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
} public static void bubbleSort(int[] arr) { for (int x=0; x<arr.length-1; x++ )
{
for (int y=0; y<arr.length-x-1; y++ )//-x:让每一次比较的次数减少,-1:避免角标越界。
{
if (arr[y]>arr[y+1])
{
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
} }
}
} public static void ArrayPrint(int[] arry) {
for (int i = 0; i < arry.length; i++ ) {
System.out.print("arry[" + i + "]= " + arry[i] + ", ");
}
}
/*
置换位置 */
public static void swap(int[] arr, int a, int b) { int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
} }
数组的查找操作
/* 数组的查找操作。 练习:将一个元素插入有序数组中,保证数组还是有序的。
*/ class ArrayTest4
{
public static void main(String[] args) { int[] arr = {2, 1, 3, 5, 4, 7, 2, 9};
int index = getIndex(arr,3);
System.out.println("Index=" + index); int[] arr2 = {2, 3, 5, 7, 8, 12, 15};
}
/*
练习:
*/
public static int getIndex_2(int[] arr, int key) {
int min, max, mid;
min = 0;
max = arr.length-1;
mid = (min + max)/2; while(min<=max)
{
mid = (min + max)>>2;
if(key > arr[mid])
min = mid + 1;
else if(key < arr[mid])
max = mid - 1;
return -1;
else
return mid;
}
return mid;
}
/*
折半查找,提高效率,但是必须要保证该数组是有序的。 */
public static int halfSearch(int[] arr, int Key)
{
int min, max, mid;
min = 0;
max = arr.length-1;
mid = (min + max)/2; while(arr[mid] != key)
{
if(key > arr[mid])
min = mid + 1;
else if(key < arr[mid])
max = mid - 1;
if (min > max)
{
return -1;
}
mid = (min + max)/2;
}
return mid;
}
/*
折半的第二种方式。
*/
public static int halfSearch_2(int[] arr, int key)
{
int min, max, mid;
min = 0;
max = arr.length-1;
mid = (min + max)/2; while(min<=max)
{
mid = (min + max)>>2;
if(key > arr[mid])
min = mid + 1;
else if(key < arr[mid])
max = mid - 1;
return -1;
else
return mid;
}
return -1;
}
//定义功能,获取key值第一次出现在数组中的位置,如果返回是-1,那么代表key在数组中不存在。
public static int getIndex(int[] arr, int key) { for (int x = 0; x<arr.length; x++ )
{
if (arr[x]==key)
{
return x;
} }
return -1;
}
}
进制转换:
class ArrayTest5
{
public static void main(String[] args)
{
toBin(6);
}
/*
十进制-->十六进制
*/
public static void toHex(int num) {
StringBuffer sb = new StringBuffer();
for (int x = 0; x<8; x++)
{
int temp = num & 15;
if (temp > 9)
{
//System.out.println((char)(temp-10+'A'));
sb.append((char)(temp-10+'A'));
} else {
//System.out.println(temp);
sb.append(temp);
}
num = num >>> 4;
}
System.out.println(sb.reverse());
}
/*
十进制转二进制
*/
public static void toBin(int num)
{
StringBuffer sb = new StringBuffer();
while(num>0)
{
sb.append(num%2);
num = num/2; }
System.out.println(sb.reverse());
}
}
/*
查表法:将所有的元素临时存储起来。建立对应关系。 */
class ArrayTest6
{
public static void main(String[] args)
{
toHex(60);
toBin(6);
} public static void toHex(int num)
{
char[] chs = {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F' };
//定义一个临时的容器
char[] arr =new char[8];
int pos = arr.length;
while(num!=0)
{
int temp = num & 15;
arr[--pos] = chs[temp]; num = num >>> 4; }
for(int x = pos; x<arr.length; x++)
{
System.out.print(arr[x]+",");
}
} public static void toBin(int num) {
char[] chs = {'0','1'}; char[] arr = new char[32];
//定义一个操作数组的指针
int pos = arr.length;
while(num != 0) {
int temp = num & 1; arr[--pos] = chs[temp];
num = num >>> 1;
} for(int x = pos; x<arr.length; x++)
{
System.out.print(arr[x]);
} }
}
二维数组

java学习——数组的更多相关文章
- Java学习-数组
1.数组的是Object的直接子类,它属于“第一类对象”,但是它又与普通的java对象存在很大的不同,类名为:[I 一维数组:[I 二维数组:[[I 三维数组:[[[I 2.[代表了数组的维度,一个[ ...
- Java学习--数组--判断数组中是否包含某个元素的方法
package zaLearnpackage; import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; import ...
- Java学习---- 数组的引用传递
1. public class ArrayRefDemo01{ public static void main(String args[]){ int temp[] = {1,3,5} ; // 利用 ...
- Java学习--数组与方法
1. public class MethodDemo01{ public static void main(String args[]){ printInfo() ; // 调用printInfo() ...
- Java学习--数组的定义和使用
1. 数组分配了空间,未赋值 public class ArrayDemo01{ public static void main(String args[]){ int score[] = null ...
- Java学习——数组的基础知识
数组的特点.分类:一维.二维数组的使用:数组的声明和初始化.调用数组的指定位置的元素.获取数组的长度.遍历数组.数组元素的默认初始化值
- java一维数组学习
/* * java学习: * 一维数组的使用: 声明语法 DataType[] name 或 DataType name[]. 初始化语法 DataType[] name = new DataType ...
- Java 学习(6):java Number & Math & String & 数组...常用类型
目录 --- Number & Math类 --- Character 类 --- String 类 --- StringBuffer 类 --- 数组 Number & Math类: ...
- Java学习笔记之---方法和数组
Java学习笔记之---方法与数组 (一)方法 (1)什么是方法? 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 (2)方法的优点 使程序变得更简短而 ...
随机推荐
- css-文字
<!DOCTYPE html>CSS2-文字 <style>div{ /*文字*/ font-size:120px; /*文字大小*/ font-family:Arial,'方 ...
- ios禁用多按钮同时点下的效果
只需要把那些不能同时点下的按钮或者视图设置一下即可. [view setExclusiveTouch:YES]; 避免view上多个button同时按下,则可设置每个button的setExclusi ...
- 最近因为textview高度问题疯了疯了疯了
1.textview有\r\n什么的就算不明白,我的文本最后一个字符是\r,结果我死活算不对,最后发现了==! NSString * str = [_messageModels[indexPath.r ...
- struts2 0day漏洞
描述 Apache Struts2 近日出现一个0day漏洞,该漏洞在修补CVE-2014-0050和2014-0094两个安全漏洞处理不当,分别可以导致服务器受到拒绝服务攻击和被执行恶意代码. 漏洞 ...
- 手机扫描二维码下载APP,根据操作系统不同自动下载
Android和IOS手机扫描二维码下载APP,根据OS不同,自动处理相应下载操作.IOS自动跳转至AppStore应用下载页,Android自动下载应用的apk包. <script type= ...
- Java 一个字符串在另外一个字符串出现次数
统计一个字符串在另外一个字符串出现次数 代码如下: package me.chunsheng.javatest; import java.util.regex.Matcher; import java ...
- PHP 中 const define 的区别
在php中定义常量时,可用到const与define这两种方法,那他们到底有什么区别呢? 1.const用于类成员变量的定义,一经定义,不可修改.define不可用于类成员变量的定义,可用于全局常量. ...
- zabbix之2安装编译/基本功能实现
1.安装方式: rpm或者编译都可,rpm可以直接用yum安装. rpm安装的话,根据文件名进行选择即可. 编译的话,不同参数对应不同的组件. 编译安装zabbix:同时安装server和agent, ...
- automation studio 6.0 破解版 32位
破解软件在iso文件的patch目录下 链接:http://pan.baidu.com/s/1o8KR7rc 密码:y87g
- 关于python的面向对象编程
先写上代码,有代码才好理解: #filename:classdemo.py class test: '''just person''' a=1 b=2 c=0 def __init__(self): ...