元素类型[] 数组名 = 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学习——数组的更多相关文章

  1. Java学习-数组

    1.数组的是Object的直接子类,它属于“第一类对象”,但是它又与普通的java对象存在很大的不同,类名为:[I 一维数组:[I 二维数组:[[I 三维数组:[[[I 2.[代表了数组的维度,一个[ ...

  2. Java学习--数组--判断数组中是否包含某个元素的方法

    package zaLearnpackage; import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; import ...

  3. Java学习---- 数组的引用传递

    1. public class ArrayRefDemo01{ public static void main(String args[]){ int temp[] = {1,3,5} ; // 利用 ...

  4. Java学习--数组与方法

    1. public class MethodDemo01{ public static void main(String args[]){ printInfo() ; // 调用printInfo() ...

  5. Java学习--数组的定义和使用

    1. 数组分配了空间,未赋值 public class ArrayDemo01{ public static void main(String args[]){ int score[] = null ...

  6. Java学习——数组的基础知识

    数组的特点.分类:一维.二维数组的使用:数组的声明和初始化.调用数组的指定位置的元素.获取数组的长度.遍历数组.数组元素的默认初始化值

  7. java一维数组学习

    /* * java学习: * 一维数组的使用: 声明语法 DataType[] name 或 DataType name[]. 初始化语法 DataType[] name = new DataType ...

  8. Java 学习(6):java Number & Math & String & 数组...常用类型

    目录 --- Number & Math类 --- Character 类 --- String 类 --- StringBuffer 类 --- 数组 Number & Math类: ...

  9. Java学习笔记之---方法和数组

    Java学习笔记之---方法与数组 (一)方法 (1)什么是方法? 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 (2)方法的优点 使程序变得更简短而 ...

随机推荐

  1. html中opacity的使用

    今天做项目要用到一个层背景透明,层上的内容不透明的效果 结果网上找了半天,没一个靠谱的, 最后倒是被一句话点醒了:纸烧了,纸上面的字也会没了 所以,要设2层遮罩层,看代码: .dialog_1//内容 ...

  2. IOS 手机端搜索硬件设备 --- 物联网

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #i ...

  3. Webform用户控件

    用户控件一 用户控件二

  4. poj1088 经典DP

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 88296   Accepted: 33100 Description ...

  5. codeforces 519E A and B and Lecture Rooms(LCA,倍增)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud E. A and B and Lecture Rooms A and B are ...

  6. No1_4.数组的基本操作_Java学习笔记

    import java.util.Arrays; public class HelloArrayOp { public static void main(String[] args) { // TOD ...

  7. ajax 传值 中文乱码问题

    使用encodeURI编码内容 var Path = encodeURI("中文.xls"); url: "ashx/Data.ashx?Path =" + P ...

  8. nginx之如何获取真实客户端ip

    nginx的配置文件中日志格式加入$http_x_forwarded_for--> log_format access '$remote_addr - $remote_user [$time_l ...

  9. mvn打包发布

    一:打包 cmd进入工作目录运行命令 1: mvn clean 2: mvn install  3: mvn clean compile    4: mvn package -DiskipTest  ...

  10. LeetCode_Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...