c++自定义数组越异常 ArrayIndexOutOfBoundsException (学习)
#include <iostream>
using namespace std;
const int DefaultSize = 10;
class Array
{
public:
Array(int itsSize=DefaultSize);
~Array()
{
delete[] pType;
}
//运算符重载
int& operator[](int offset);
const int& operator[](int offset) const;
int GetItsSize() const
{
return itsSize;
}
class ArrayIndexOutOfBoundsException {};
class ElementZero{};
private:
int *pType;
int itsSize;
};
Array::Array(int size) :itsSize(size)
{
if (size==0)
{
throw ElementZero();
}
pType = new int[size];
for (int i=0;i<size;i++)
{
pType[i] = 0;
}
}
int& Array::operator[](int offset)
{
int vcsize =GetItsSize();
if (offset>=0 && offset<vcsize)
{
return pType[offset];
}else{
throw ArrayIndexOutOfBoundsException();
}
}
const int& Array::operator[](int offset) const
{
int vcsize = this->GetItsSize();
if (offset >= 0 && offset<vcsize)
{
return pType[offset];
}
else {
throw ArrayIndexOutOfBoundsException();
}
}
int main()
{
Array a;
Array b(12);
b[2] = 10;
cout << b[2]<< endl;
Array arr1(20);
try
{
for (int k=0;k<100;k++)
{
arr1[k] = k;
}
}
catch (Array::ArrayIndexOutOfBoundsException)
{
cout<<"Array Index Out Of Bounds Exception..."<<endl;
}
system("pause");
return 0;
}
-------------------------------------------------------------------------------------
10
Array Index Out Of Bounds Exception...
请按任意键继续. . .
c++自定义数组越异常 ArrayIndexOutOfBoundsException (学习)的更多相关文章
- C#学习之自定义数组及其排序
在C#中对数组的定义比较灵活.这里着重说一下自定义数组和Array类的排序. 在Array类中通过属性Length就可以获取整个数组中数据的数量,可以通过foreach迭代数组. 使用Rank属性可以 ...
- Java中的自定义数组队列
在Java中,作为所有数据结构中存储和获取速度最快的一种,数组凭借其这种简单易用的优势在各个方面都能大显神威.但是数组也有自身的局限性.数组的长度必须是固定的一旦定义之后就无法动态的更改,这就会造成这 ...
- 墨菲定律与 IndexOutOfBoundsException(数组越界异常)
今天维护又反馈了一问题过来,查询试卷时报数组越界异常: 2017-02-28 10:45:24,827[ERROR] HttpException[10.32.111.7:60446:2D07867BE ...
- Java实现自定义数组及其方法
自定义数组 主要功能有增.删(根据索引,根据值).改.查扩容等功能 package array; public class CustomArray { private int[] array = nu ...
- C++ 数组的地址问题学习随笔
二维数组额地址问题学习,本文学习内容参考:http://blog.csdn.net/wwdlk/article/details/6322439 #include<iostream> usi ...
- yii CListView中使用CArrayDataProvider自定义数组作为数据
CArrayDataProvider类手册: http://www.yiichina.com/api/CArrayDataProvider 在yii中无论是CListView还是CGridView,对 ...
- 5.java.lang.IndexOutOfBoundsException(数组下标越界异常)
数组下标越界异常 查看调用的数组或者字符串的下标值是不是超出了数组的范围,一般来说,显示(即直接用常数当下标)调用不太容易出这样的错,但隐式(即用变量表示下标)调用就经常出错了,还有一种情况,是程序中 ...
- python3.4中自定义数组类(即重写数组类)
'''自定义数组类,实现数组中数字之间的四则运算,内积运算,大小比较,数组元素访问修改及成员测试等功能''' class MyArray: '''保证输入值为数字元素(整型,浮点型,复数)''' de ...
- 学JAVA二十一天,自定义数组
今天就说一下自定义数组,至于要怎么用,我也不知道,反正逼格挺高的. 闲话不多说,开始: 首先,自定义数组首先要创建一个类,用来做自定义数组的类型. public class User{ private ...
随机推荐
- 搭建KVM环境——07 带GUI的Linux上安装KVM图形界面管理工具
清空yum源缓存,并查看yun源 [root@CentOS2 ~]# yum clean all Loaded plugins: fastestmirror, langpacks Cleaning r ...
- Mha-Atlas-MySQL高可用方案实践
一,mysql-mha环境准备 1.1 实验环境: 1.1 实验环境: 主机名 IP地址(NAT) 描述 mysql-master eth0:10.1.1.154 系统:CentOS6.5(6.x都可 ...
- Paper Reading:Mask RCNN
Mask RCNN 论文:Mask R-CNN 发表时间:2018 发表作者:(Facebook AI Research)Kaiming He, Georgia Gkioxari, Piotr Dol ...
- 0012SpringBoot访问首页
有三种方式可以实现访问首页: 第一种: 定义一个Controller,定义请求方法和返回内容,方法内容如下: @RequestMapping({"/","/index&q ...
- MATLAB中.m文件生成.dll
1.配置编译环境 在命令行窗口输入: mbuild -setup 如果出现以下提示信息说明成功: 如果提示信息为: 错误使用mbuild(line 164) Unable to complete su ...
- ASP.NET MVC Liu_Cabbage 个人博客
RightControl_Blog 介绍 前台使用燕十三博客前端模板,后台基于RightControl .NET通用角色权限管理系统搭建,已完成.项目地址:http://www.baocaige.to ...
- Idea中提交SVN或git时,忽略某些文件不提交
第一步:点击 setting 第二步:点击Editor下的File Types 第三步:编辑,在后面添加 *.iml;*.idea;*.gitignore;*.sh;*.classpath;*.pro ...
- mysql sql语句多表合并UNION ALL和UNION
select d1.ID,CAST(d1.ID AS CHAR) AS intId, d1.CODE_TYPE, d1.CODE, d1.CODE_IMG, d1.VALUE from m_dict_ ...
- Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树
题解看这里 liouzhou_101的博客园 更简洁的代码看这里: #include <bits/stdc++.h> using namespace std; typedef long l ...
- [Google Guava] 2.4-集合扩展工具类
原文链接 译文链接 译者:沈义扬,校对:丁一 简介 有时候你需要实现自己的集合扩展.也许你想要在元素被添加到列表时增加特定的行为,或者你想实现一个Iterable,其底层实际上是遍历数据库查询的结果集 ...