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)方法的优点 使程序变得更简短而 ...
随机推荐
- 细讲encodeURI和encodeURIComponent以及escape的区别与应用
首先,我们都知道这三个东西都是用来编码的 先来说encodeURI()和encodeURIComponent() 这两个是在转换url时候用来编码解码用的. 有编码就会有解码, 解码就是decodeU ...
- asp.net repeater控件操作
Repeater控件和DataList控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行. Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式. ...
- Python学习笔记3(数据结构)
1.元组结构(Tuple) 元组由不同的元素组成,每个元素可以存储不同类型的数据,如字符串.数字甚至元组.元组创建后不能修改. 元组通常代表一行数据,而元组中的元素代表不同的数据项. 1.1元组的创建 ...
- Autorelease Pool-自动释放池
Autorelease Pool是Objective-C中的内存管理方式之一,它与线程和NSAutorelease类有关.每一个线程都拥有自己的Autorelease Pool栈,这个栈底层是由双向链 ...
- Draw2d中的布局管理器Layout比较
最近在研究Eclipse中的GEF开发,在跟着GEF-whole-upload教程做一个GEF应用程序的例子时,发现Figure上的控件无法显示,谷歌了很久也没找到解决方案,最后终于发现是Layout ...
- OFBiz应用https与http方式访问切换
url.properties port.https.enabled=N port.https=8444 force.https.host=
- WPF/MVVM 快速开发
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial 这篇文章醍醐灌顶,入门良药啊! Introductio ...
- 解决pip安装时 UnicodeDecodeError 问题
在装django的时候用pip安装就出现了问题,一大堆的红字,然后联想到很多次用pip安装都以失败告终,于是今天抽空看了看出现问题的原因. 情况如下图:
- oc语言--内存管理
一.基本原理 1.什么是内存管理 1> 移动设备的内存及其有限,每个app所能占用的内存是有限制的 2> 当app所占用的内存较多时,系统就会发出内存警告,这是需要回收一些不需要的内存空间 ...
- LINUX SSH客户端的中文乱码问题
原因在于文件/etc/sysconfig/i18n 这个文件是系统的区域语言设置, i18n是 国际化internationalization的缩写 i和n之间正好18个字母 解释: LANG= ...