MyArrayList myArrayList = new MyArrayList(); myArrayList.add(1); myArrayList.add(2); myArrayList.add(3); myArrayList.display(); /*myArrayList.add(5); myArrayList.display();*/ try{ myArrayList.add(1,99); }catch (AddIndexOutOfException e) { e.printStackTrace(); } myArrayList.display(); }怎么给他横着打印
时间: 2024-03-04 20:49:07 浏览: 118
您可以将 `MyArrayList` 类中的 `display()` 方法进行修改,改为横向打印元素。以下是一种可能的实现方式:
```java
public void display() {
if (size == 0) {
System.out.println("[]");
return;
}
System.out.print("[");
for (int i = 0; i < size - 1; i++) {
System.out.print(elementData[i] + ", ");
}
System.out.println(elementData[size - 1] + "]");
}
```
这个实现方式在打印元素时,首先输出一个左中括号 `[`,然后依次输出每个元素和一个逗号 `,`,最后输出一个右中括号 `]`。这样就能够横向打印元素了。
相关问题
将以下程序补充完整 #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类,要求按成绩排序(从高到低)
将以下程序补充完整:
```
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 temp = mpArr[j];
mpArr[j] = mpArr[j+1];
mpArr[j+1] = temp;
}
}
}
}
template <typename ElemType>
myArrayList<ElemType>::myArrayList(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 =(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;
}
//修改补充 Student类,要求按成绩排序(从高到低)
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<<" Score:"<<s.score<<endl;
return out;
}
};
```
说明:
- `sort()`函数实现排序功能,采用冒泡排序算法;
- `myArrayList`类的拷贝构造函数和赋值运算符重载函数均实现深拷贝功能;
- `Student`类的 `<` 运算符重载函数改为按成绩从高到低排序。
public MyArrayList() { array = (T[]) new Object[10]; size = 0; }
这段代码是一个泛型类MyArrayList的构造函数。在构造函数中,首先创建了一个大小为10的Object类型的数组array,然后通过强制类型转换将其转换为一个泛型类型T的数组。最后将size初始化为0,表示当前列表中没有元素。由于Java中的泛型擦除机制,实际上在运行时,T将被替换为Object类型。
阅读全文