前提
クラスの先頭で配列
static int count = 0;
Position[] arrayposition = new Position[count +1];
を宣言し、それをメソッドaddPosition()で配列の値を定義しました。
その上で同じクラス内の別のメソッドfindPositionでその値を利用したいのですが、どのようにすれば利用することが可能でしょうか。
<エラーメッセージ>
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Portfolio.addPosition(Issue4.java:223)
at Issue100.main(Issue4.java:32)
実際に書いたコードはこちらです。
また全部のコードも記載致します。
java
1コード 2class Portfolio extends Position{ 3 static int count = 0; 4 Position[] arrayposition = new Position[count +1]; 5 6 public void addPosition(Position position){ 7 if(count == 0){ 8 arrayposition[count] = position; 9 count++; 10 } 11 else if(count > 0){ 12 for(int i = 0; count > i;i++){ 13 boolean bool = position.equals(arrayposition[i]); 14 if(bool == true){ 15 amount++; 16 break; 17 } 18 else if(bool == false){ 19 if(i == count -1){ 20 arrayposition[count] = position; 21 count++; 22 } 23 } 24 } 25 } 26 } 27 28 public Position findPosition(Issue4 issue){ 29 boolean bool2; 30 for(int i = 0;i < count + 1;i++){ 31 bool2 = issue.equals(arrayposition[i]); 32 if(bool2 == true){ 33 return arrayposition[i]; 34 } 35 else{ 36 } 37 } 38 return null; 39 } 40 41 @Override 42 public int hashCode(){ 43 int result = 1; 44 result = 31*result + ((issue == null)? 0:issue.hashCode()); 45 return result; 46 } 47 @Override 48 public boolean equals(Object obj){ 49 if(this == obj){ 50 return true; 51 } 52 if(obj == null){ 53 return false; 54 } 55 if(getClass() != obj.getClass()){ 56 return false; 57 } 58 Portfolio port = (Portfolio)obj; 59 60 if(!issue.equals(port.issue)){ 61 return false; 62 } 63 return true; 64 } 65} 66java 67コード全体 68import java.io.*; 69 70class Issue100{ 71 public static void main(String[] args){ 72 73 Issue4 issue1 = new Issue4("1111","AAAA"); 74 Issue4 issue2 = new Issue4("2222","BBBB"); 75 Issue4 issue3 = new Issue4("3333","CCCC"); 76 Issue4 issue4 = new Issue4("4444","DDDD"); 77 Issue4 issue5 = new Issue4("5555","EEEE"); 78 Issue4 issue6 = new Issue4("6666","FFFF"); 79 Issue4 issue7 = new Issue4("1111","AAAA"); 80 81 Stock stock1 = new Stock("1111","AAAA",Market.TSE); 82 Stock stock2 = new Stock("2222","BBBB",Market.OSE); 83 Stock stock3 = new Stock("3333","CCCC",Market.NSE); 84 Bond bon1 = new Bond("1111","AAAA",20001111,1.0); 85 Bond bon2 = new Bond("2222","BBBB",20002222,1.5); 86 Bond bon3 = new Bond("3333","CCCC",20003333,2.0); 87 88 89 Position pos1 = new Position(issue1,0); 90 Position pos2 = new Position(issue2,0); 91 Position pos3 = new Position(issue3,0); 92 Position pos4 = new Position(issue4,0); 93 Position pos5 = new Position(issue5,0); 94 Position pos6 = new Position(issue6,0); 95 96 Portfolio por = new Portfolio(); 97 98 por.addPosition(pos1); 99 por.addPosition(pos2); 100 /* 101 Position a = por.findPosition(issue7); 102 System.out.println(a); 103 */ 104 } 105} 106class Issue4{ 107 108 protected String code; 109 protected String name; 110 111 public Issue4(String code,String name)throws IllegalArgumentException{ 112 if(code == null || name == null){ 113 IllegalArgumentException e = new IllegalArgumentException(); 114 throw e; 115 } 116 else{ 117 this.code = code; 118 this.name = name; 119 } 120 } 121 public String getCode(){ 122 return this.code; 123 } 124 public String getName(){ 125 return this.name; 126 } 127} 128 129class Bond extends Issue4{ 130 131 private int maturity; 132 private double coupon; 133 134 public Bond(String code,String name,int maturity,double coupon)throws IllegalArgumentException{ 135 super(code,name); 136 if(maturity <= 20000101 || maturity >= 29991231){ 137 IllegalArgumentException e = new IllegalArgumentException(); 138 throw e; 139 } 140 else{ 141 if(coupon < 0){ 142 IllegalArgumentException e = new IllegalArgumentException(); 143 throw e; 144 } 145 else{ 146 this.maturity = maturity; 147 this.coupon = coupon; 148 } 149 } 150 } 151 public int getMaturity(){ 152 return this.maturity; 153 } 154 public double getCoupon(){ 155 return this.coupon; 156 } 157 public BondType getBondType(){ 158 if(coupon == 0){ 159 BondType bondtype = BondType.ZERO_COUPON_BOND; 160 return bondtype; 161 } 162 else{ 163 BondType bondtype = BondType.COUPON_BOND; 164 return bondtype; 165 } 166 } 167 @Override 168 public String toString(){ 169 return "コード :" + code + "\n" + "名称 :" + name + "\n" + "償還月日 :"+ this.maturity + 170 "\n" + "クーポンレート :" + this.coupon ; 171 } 172 @Override 173 public int hashCode(){ 174 double result = 1; 175 result = 31*result + ((code == null)? 0:code.hashCode()); 176 result = 31*result + this.maturity; 177 result = 31*result + this.coupon; 178 int result2 = (int)result; 179 return result2; 180 } 181 @Override 182 public boolean equals(Object obj){ 183 if(this == obj){ 184 return true; 185 } 186 if(obj == null){ 187 return false; 188 } 189 if(getClass() != obj.getClass()){ 190 return false; 191 } 192 Bond bond = (Bond)obj; 193 194 if(!code.equals(bond.code)){ 195 return false; 196 } 197 if(maturity != bond.maturity){ 198 return false; 199 } 200 if(coupon != bond.coupon){ 201 return false; 202 } 203 return true; 204 } 205} 206class Stock extends Issue4{ 207 private Market market; 208 209 public Stock(String code,String name,Market market){ 210 super(code,name); 211 this.market = market; 212 } 213 public Market getMarket(){ 214 return this.market; 215 } 216 @Override 217 public String toString(){ 218 return "コード :" + code + "\n" + "名称 :" + name + "\n" + "上場市場 :"+ this.market; 219 } 220 @Override 221 public int hashCode(){ 222 int result = 1; 223 result = 31*result + ((code == null)? 0:code.hashCode()); 224 result = 31*result + ((this.market == null)? 0:market.hashCode()); 225 return result; 226 } 227 @Override 228 public boolean equals(Object obj){ 229 if(this == obj){ 230 return true; 231 } 232 if(obj == null){ 233 return false; 234 } 235 if(getClass() != obj.getClass()){ 236 return false; 237 } 238 Stock stock = (Stock)obj; 239 240 if(!code.equals(stock.code)){ 241 return false; 242 } 243 if(market != stock.market){ 244 return false; 245 } 246 return true; 247 } 248} 249class Position{ 250 251 protected Issue4 issue; 252 protected double amount; 253 254 public Position(){ 255 } 256 public Position(Issue4 issue,double amount){ 257 this.issue = issue; 258 this.amount = amount; 259 } 260 public Issue4 getIssue4(){ 261 return this.issue; 262 } 263 public double getAmount(){ 264 return this.amount; 265 } 266 public void setIssue4(Issue4 issue){ 267 this.issue = issue; 268 } 269 public void setAmount(double amount){ 270 this.amount = amount; 271 } 272} 273class Portfolio extends Position{ 274 static int count = 0; 275 Position[] arrayposition = new Position[count +1]; 276 277 public void addPosition(Position position){ 278 if(count == 0){ 279 arrayposition[count] = position; 280 count++; 281 } 282 else if(count > 0){ 283 for(int i = 0; count > i;i++){ 284 boolean bool = position.equals(arrayposition[i]); 285 if(bool == true){ 286 amount++; 287 break; 288 } 289 else if(bool == false){ 290 if(i == count -1){ 291 arrayposition[count] = position; 292 count++; 293 } 294 } 295 } 296 } 297 } 298/* 299 public Position findPosition(Issue4 issue){ 300 boolean bool2; 301 for(int i = 0;i < count + 1;i++){ 302 bool2 = issue.equals(arrayposition[i]); 303 if(bool2 == true){ 304 return arrayposition[i]; 305 } 306 else{ 307 } 308 } 309 return null; 310 } 311 */ 312 @Override 313 public int hashCode(){ 314 int result = 1; 315 result = 31*result + ((issue == null)? 0:issue.hashCode()); 316 return result; 317 } 318 @Override 319 public boolean equals(Object obj){ 320 if(this == obj){ 321 return true; 322 } 323 if(obj == null){ 324 return false; 325 } 326 if(getClass() != obj.getClass()){ 327 return false; 328 } 329 Portfolio port = (Portfolio)obj; 330 331 if(!issue.equals(port.issue)){ 332 return false; 333 } 334 return true; 335 } 336}
回答2件