class myArrayList { private: int mSize; int mLen; ElemType *mpArr;class myArrayList { private: int mSize; int mLen; ElemType *mpArr;
时间: 2024-05-23 10:10:39 浏览: 14
这是一个 C++ 类的定义,名为 `myArrayList`。这个类包含了三个私有成员变量:`mSize` 表示这个数组的总大小,`mLen` 表示这个数组当前的长度,`mpArr` 是一个指向 `ElemType` 类型的指针,表示这个数组的首地址。`ElemType` 可以是任意类型,由程序员自己定义。
这个类的作用是实现一个动态数组,可以动态地添加、删除元素,同时也可以访问和修改指定位置的元素。
相关问题
将以下程序补充完整 #include <iostream> #include <fstream> #include <iomanip> using namespace std; template <typename ElemType> class myArrayList { private: int mSize; int mLen; ElemType *mpArr; public: myArrayList(int n); myArrayList(ElemType *a,int n); void show(); ElemType getMax(); //以下函数由同学完成 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> & operator =(mymyArrayList<ElemType> &other) }; template <typename ElemType> myArrayList<ElemType>::myArrayList(int n) { this->mSize=n; this->mLen=0; this->mpArr=new ElemType[mSize]; } template <typename ElemType> myArrayList<ElemType>::myArrayList(ElemType *a,int n) { this->mSize=n; this->mLen=n; this->mpArr=new ElemType[mSize]; for(int i=0;i<mLen;i++) mpArr[i]=a[i]; } template <typename ElemType> void myArrayList<ElemType>::show() { for(int i=0;i<mLen;i++) cout<<setw(4)<<mpArr[i]; cout<<endl; } template <typename ElemType> ElemType myArrayList<ElemType>::getMax() { ElemType max; max=mpArr[0]; for(int i=1;i<mLen;i++) if(max<mpArr[i]) max=mpArr[i]; return max; } //Student.h class Student { private: int mId; float height; int score; public: Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { } friendbool operator <(Student &a1,Student &a2) { if(a1.height<a2.height) return true; else return false; } friend ostream &operator <<(ostream &out,Student &s) { out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl; return out; } }; //主程序 int main() { int a[]={1,2,3,5,7,9,12,8}; double b[]={1,2.5,3.6,5,7,9,12.8,8}; myArrayList <int> list1(a,8); list1.show(); cout<<"max="<<list1.getMax()<<endl; myArrayList <double> list2(b,8); list2.show(); cout<<"max="<<list2.getMax()<<endl; Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1; myArrayList <Student> list3(s,3); list3.show(); cout<<"max="<<list3.getMax()<<endl; } 说明: 将以下程序补充完整 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> operator =(mymyArrayList<ElemType> &other) //修改补充 Student类,要求按成绩排序(从高到低)并将完整的代码发出来
以下是补充完整的程序:
```
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
template <typename ElemType>
class myArrayList {
private:
int mSize;
int mLen;
ElemType *mpArr;
public:
myArrayList(int n);
myArrayList(ElemType *a,int n);
void show();
ElemType getMax();
void sort();
myArrayList(const myArrayList<ElemType> &other);
myArrayList<ElemType> & operator =(const myArrayList<ElemType> &other);
};
template <typename ElemType>
myArrayList<ElemType>::myArrayList(int n) {
this->mSize=n;
this->mLen=0;
this->mpArr=new ElemType[mSize];
}
template <typename ElemType>
myArrayList<ElemType>::myArrayList(ElemType *a,int n) {
this->mSize=n;
this->mLen=n;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=a[i];
}
template <typename ElemType>
void myArrayList<ElemType>::show() {
for(int i=0;i<mLen;i++)
cout<<setw(4)<<mpArr[i];
cout<<endl;
}
template <typename ElemType>
ElemType myArrayList<ElemType>::getMax() {
ElemType max;
max=mpArr[0];
for(int i=1;i<mLen;i++)
if(max<mpArr[i])
max=mpArr[i];
return max;
}
template <typename ElemType>
void myArrayList<ElemType>::sort() {
for(int i=0;i<mLen-1;i++) {
for(int j=0;j<mLen-i-1;j++) {
if(mpArr[j]<mpArr[j+1]) {
ElemType tmp=mpArr[j];
mpArr[j]=mpArr[j+1];
mpArr[j+1]=tmp;
}
}
}
}
template <typename ElemType>
myArrayList<ElemType>::myArrayList(const myArrayList<ElemType> &other) {
this->mSize=other.mSize;
this->mLen=other.mLen;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=other.mpArr[i];
}
template <typename ElemType>
myArrayList<ElemType> & myArrayList<ElemType>::operator =(const myArrayList<ElemType> &other) {
if(this==&other)
return *this;
delete []mpArr;
this->mSize=other.mSize;
this->mLen=other.mLen;
this->mpArr=new ElemType[mSize];
for(int i=0;i<mLen;i++)
mpArr[i]=other.mpArr[i];
return *this;
}
class Student {
private:
int mId;
float height;
int score;
public:
Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { }
friend bool operator <(Student &a1,Student &a2) {
if(a1.score<a2.score)
return true;
else
return false;
}
friend ostream &operator <<(ostream &out,Student &s) {
out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl;
return out;
}
};
int main() {
int a[]={1,2,3,5,7,9,12,8};
double b[]={1,2.5,3.6,5,7,9,12.8,8};
myArrayList <int> list1(a,8);
list1.show();
cout<<"max="<<list1.getMax()<<endl;
myArrayList <double> list2(b,8);
list2.show();
cout<<"max="<<list2.getMax()<<endl;
Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1;
myArrayList <Student> list3(s,3);
list3.show();
list3.sort();
cout<<"max="<<list3.getMax()<<endl;
list3.show();
return 0;
}
```
在这个程序中,我们添加了以下功能:
- `sort()`函数用于对数组进行排序
- 拷贝构造函数 `myArrayList(const myArrayList<ElemType> &other)`用于创建新的对象并将其初始化为与另一个对象相同的值
- 赋值运算符 `myArrayList<ElemType> & operator =(const myArrayList<ElemType> &other)`用于将一个对象的值赋给另一个对象
此外,我们还修改了`Student`类的实现,使其能够按照成绩从高到低进行排序。
参照ArrayList类的功能,创建自己的ArrayList类:MyArrayList.
好的,让我们开始创建 MyArrayList 类。
```java
public class MyArrayList<E> {
private static final int DEFAULT_CAPACITY = 10;
private Object[] elementData;
private int size = 0;
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
public MyArrayList(int initialCapacity) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "
+ initialCapacity);
elementData = new Object[initialCapacity];
}
public void add(E e) {
if (size == elementData.length) {
ensureCapacity();
}
elementData[size++] = e;
}
@SuppressWarnings("unchecked")
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
return (E) elementData[index];
}
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
E removedElement = get(index);
for (int i = index; i < size - 1; i++) {
elementData[i] = elementData[i + 1];
}
elementData[--size] = null;
return removedElement;
}
public int size() {
return size;
}
private void ensureCapacity() {
int newCapacity = elementData.length * 2;
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
```
这里我们使用了泛型,这样就可以让 MyArrayList 类型安全地存储任何类型的对象。我们还定义了两个构造函数,一个是使用默认容量创建 MyArrayList 对象,另一个是可以指定容量的构造函数。
add() 方法用于将元素添加到 MyArrayList 中。如果 MyArrayList 已满,我们就需要扩容,这里我们使用了一个 ensureCapacity() 方法来实现。get() 方法用于获取指定索引位置上的元素,remove() 方法用于删除指定索引位置上的元素,size() 方法用于获取 MyArrayList 中元素的数量。
这就是我们的 MyArrayList 类的实现。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)