/**
* 编写一个数组类,实现如下功能:
* (1)容量固定,不可改变(2)给任意位置的元素赋值(3)获得任意位置的元素(4)数组持有的元素个数满了,给提示
*/
public class Array {
// 记录数组持有的元素的引用数组
private int[] elementData;
// 记录数组持有的元素的个数;
private int size;
// 数组的容量;
private int Capacity;
// 构造函数:初始化
public Array(int N){
elementData = new int[N];
Capacity = N;
size = 0;
}
/**
* 给任意位置的元素赋值
* @param element
* @param pos
*/
public int assign(int element, int pos){
if (pos < 0 || pos > Capacity -1) {
System.out.println("参数错误");
return -1;
}
size = size + 1;
elementData[pos] = element;
return 1;
}
/**
* 获得指定位置的元素
* @param pos
* @return
*/
public int get(int pos){
if (pos < 0 || pos > Capacity -1) {
System.out.println("参数错误");
return -1;
}
if (size == Capacity) {
System.out.println("数组满了");
}
return elementData[pos];
}
* 编写一个数组类,实现如下功能:
* (1)容量固定,不可改变(2)给任意位置的元素赋值(3)获得任意位置的元素(4)数组持有的元素个数满了,给提示
*/
public class Array {
// 记录数组持有的元素的引用数组
private int[] elementData;
// 记录数组持有的元素的个数;
private int size;
// 数组的容量;
private int Capacity;
// 构造函数:初始化
public Array(int N){
elementData = new int[N];
Capacity = N;
size = 0;
}
/**
* 给任意位置的元素赋值
* @param element
* @param pos
*/
public int assign(int element, int pos){
if (pos < 0 || pos > Capacity -1) {
System.out.println("参数错误");
return -1;
}
size = size + 1;
elementData[pos] = element;
return 1;
}
/**
* 获得指定位置的元素
* @param pos
* @return
*/
public int get(int pos){
if (pos < 0 || pos > Capacity -1) {
System.out.println("参数错误");
return -1;
}
if (size == Capacity) {
System.out.println("数组满了");
}
return elementData[pos];
}