数组,队列,泛型

数组注意:

数组是引用数据类型 是一种线性结构

length为其唯一的属性 下标控制操作

一维数组

实例化数组

实例化数组的几种类型---实例化后要想赋值 必须循环

数据类型[] 数组名 = new 数据类型[长度];-----声明的同时实例化

数据类型[] 数组名 ;
数组名== new 数据类型[长度];-------先声明后实例化


赋值的几种类型

数据类型[] 数组名 = new 数据类型[长度]{值...}------实例化并赋值

数据类型[] 数组名 ={值...}----直接赋值开辟空间

数据类型[] 数组名 ;
数组名== new 数据类型[长度]{值...};-------先声明后实例化赋值

二维数组
实例化方面与赋值一维数组无异 只要是[]改为2个即可 还有{值}也要有行列之分

差异之处:
一维数组数组名.length直接表示数组长度
二二维数组里面数组名.length表示 行数 数据名[].length表示列数


优势
访问速度快 能存储多个同类型的数据

劣势
只能存储同一类型数据
空间不能加载或缩短
只有相同类型的数组才可以赋给对方


队列

一种类 封装对数组的操作 解决数组中空间问题 只能存储一种数据类型的问题

使用泛型E这个概念实现多种数据存储


任务:
1.随机2000-3000之间的数据,存入到一维数组中,并且使用三种方式对数组排序。
2.实例化一个二维数组,给数组中的每一个元素赋值,然后找出最大值的元素,但是记住不要改变数组。
3.实现一个通用的队列
4.数组和队列总结

1 随机2000-3000之间的数据,存入到一维数组中,并且使用三种方式对数组排序。
思路 首先创建一个数组对象
然后 利用循环和随机函数对一维数组赋值

插入排序思路
外层循环选取元素 从第二个元素开始 到最后一个元素
选取一个元素为当前元素进入内存循环
假设前面的元素是有序的 将这个元素与其前面的比较若大的话内存循环
结束 若小的继续比较直到大于它前面的元素

冒泡排序 两两比较 依次交换
外层循环控制趟数一共是此数组的长度-1次 内存循环控制进行两两比较
第一次将最大的数排好序 再是第二大的 按照此规则 依次排序

选择排序
将当前元素下标赋给一个变量 然后下标为变量的元素与其他元素比较 将较小的元素小标 的放入到变量中 最后判断变量和元素的小标的和当前元素下标是否相同 使得当前元素是最 小的
		
package cn.tjn.study0713;

import java.util.Random;

public class Text1Array {

public static void main(String[] args) {
int array[] = creatArray(10);
print(array);
System.out.println("===================插入排序=================");
charu(array);
print(array);
System.out.println("===================冒泡排序=================");
maopao(array);
print(array);
System.out.println("===================选择排序=================");
xuanze(array);
print(array);
}

public static int[] creatArray(int length) {
int array[] = new int[length];
Random rand = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = rand.nextInt(1000) + 2000;
}
return array;
}

public static void print(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
System.out.println();
}

// 直接插入排序
public static void charu(int[] array) {
for (int i = 1; i < array.length; i++) {
int k = array[i];
for (int j = i; j >= 1; j--) {
if (k < array[j - 1]) {
array[j] = array[j-1];
array[j-1] = k;
}
}
}
}

// 冒泡排序
public static void maopao(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}

// 选择排序

public static void xuanze(int[] array) {
for (int i = 0; i < array.length - 1; i++) {

int k = i;

for (int j = i+1; j < array.length; j++) {

if (array[j] < array[k]) {
k = j;
}

if (k != i) {
int temp = array[i];
array[i] = array[k];
array[k] = temp;
}
}
}
}
}


2.实例化一个二维数组,给数组中的每一个元素赋值,然后找出最大值的元素,但是记住不要改变数组。


思路 实例化一个二维数组 并利用2层循环赋值
再利用循环找出最大值 具体 首先将第一个元素假设为最大的赋给变量 然后利用循环使表量和其他的元素比较 将较大的放入变量中 最后输出

ackage cn.tjn.study0713;

import java.util.Random;

public class Text2Array {


public static void main(String[] args) {
int[][] array = creat(5,5);
print(array);
max(array);

}
public static int[][] creat(int row, int column){
int[][] array = new int [row][column];
Random rand = new Random();
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
array[i][j]=rand.nextInt(100);
}
}
return array;
}
public static void max(int[][] array){
int k=array[0][0];
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
if(k<=array[i][j]){
k=array[i][j];
}
}
}
System.out.println(k);
}


public static void print(int[][] array){
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
}


3.实现一个通用的队列
首先定义一个接口 写好所有要在队列中要实现的方法 然后还需要一个测试类
看队列中的中的方法是否正确
注意原数组的实例化 在队列中的构造函数 要区分 obl.length 和 initcount的区别
obl.length表示数组的长度 initcount新增元素的个数加上构造函数给的已有元素个数



package cn.tjn.study0713;

/**
* 自定义一个队列类SamlpList
*
* @author Administrator
*
*/
public class SampleList<E> implements Spl<E> {
// 定义一个私有的数组对象属性
private Object[] obl = null;
// 记录队列中已经存储了多少个元素
private int initCount = 0;
private int incresCount = 1;

// 使用默认参数构造队列
public SampleList() {
obl = new Object[0];
}

// 创建时指定队列的初始化大小
public SampleList(int initCount) {
this.initCount = initCount;
obl = new Object[initCount];
}

// 创建时指定队列的初始化大小,和每次增长比率
public SampleList(int initCount, int incresCount) {
this.initCount = initCount;
obl = new Object[initCount];
this.incresCount = incresCount;
}

// 向队列中添加一个元素 队列长度+1
public void add(E e) {
Object[] temp = new Object[obl.length + incresCount];
for (int i = 0; i < obl.length; i++) {
temp[i] = obl[i];
}
temp[obl.length] = e;
this.initCount++;
obl = temp;
}

// 将另一个队列中的所有对象加到队列的后面
public void addall(SampleList<E> list) {
Object[] temp = new Object[obl.length + list.size()];
for (int i = 0; i < obl.length; i++) {
temp[i] = obl[i];
}
for (int i = 0; i < list.size(); i++) {
temp[i + obl.length] = list.get(i);
this.initCount++;
}
obl = temp;
}

// 在队列中的指定索引位置删除一个元素 并返回该元素 队列长度-1
public E delete(int index) {
E item = (E) obl[index];
for (int i = index; i < obl.length; i++) {
obl[i] = obl[i + 1];
}
this.initCount--;
return item;
}

// 获取指定索引位置的元素
public E get(int index) {
if (index < 0 || index > initCount)
return null;
E item = (E) obl[index];
return item;
}

// 在队列的指定位置插入一个对象
public void insert(E str, int index) {
obl[index] = str;
}

// 获取队列中元素总数的方法
public int size() {
return initCount;
}
}




package cn.tjn.study0713;

/**
* 自定义泛型队列的接口
*
* @author NetJava
*
*/
public interface Spl<E> {
// 在队列的指定位置插入一个对象
public void insert(E str, int index);

// 向队列中添加一个元素 队列长度+1
public void add(E str);

// 获取指定索引位置的元素
public E get(int index);

// 获取队列中元素总数的方法
public int size();

// 在队列中的指定索引位置删除一个元素 并返回该元素 队列长度-1
public E delete(int index);

// 将另一个队列中的所有对象加到队列的后面
public void addall(SampleList<E> list);
}



package cn.tjn.study0713;
/**定义一个测试类
* @author
*/
public class Splmain {

/**
* 主函数
*/

public static void main(String[] args) {
//实例化二个对象一个传参数一个不传参数
SampleList spll1= new SampleList();
SampleList spll2= new SampleList(5);
//向队列对象spll1中添加2个元素一个为整形一个为字符串
String str1="添加无参构造的字符串元素";
int i1=5;
spll1.add(str1);
spll1.add(i1);

//打印
for(int i=0;i<spll1.size();i++){
System.out.println(spll1.get(i));
}
System.out.println("spll1队列的长度是:"+spll1.size());
System.out.println("===========================================");

String str2="添加有参构造的字符串元素";
int i2=3;
spll2.add(str2);
spll2.add(i2);
//打印
for(int i=0;i<spll1.size();i++){
System.out.println(spll2.get(i));
}
System.out.println("spll2队列的长度是:"+spll2.size());
System.out.println("===========================================");

spll2.addall(spll1);
//打印
for(int i=0;i<spll2.size();i++){
System.out.println(spll2.get(i));
}
System.out.println(spll2.size());
System.out.println("===========================================");
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值