Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am wondering if there is an easy way to turn my java AlexanderTest program from an array into an array list so I can do things such as add, remove, search for data in the list. Is there an easy way for me to convert my program from using an array to array lists and still get the same output?

I tried to implement the array list but I am not sure I did it correctly. I probably messed up trying to implement the array lists and could use some help in figuring out how to do it correctly. Can anyone help me to figure out how I can implement array lists into my program easily? I have not used array lists much and am still figuring out how they work so I probably messed some stuff up in attempting to implement them.

I tried to implement the array list but I am not sure I am doing it correctly. Currently when I run the program I get an indexoutofbounds error Can anyone take a look at my code and help me out?

Alexander_Circle program
https://pastebin.com/8Uagcp14

Alexander_Rectangle program
https://pastebin.com/NmxmrEzR

Shape program (superclass has (x, y) coordinates for a point for the shapes)
https://pastebin.com/zt5uKDxM

AlexanderTest (arrays)
https://pastebin.com/FjzfeKcN



Current output example with AlexanderTest (arrays) which is what I also want the the array lists program:

How many objects you'd like to create? 2
Enter the type of an element you'd like to create (r, c): r
Enter center coordinates of your r #1 (x, y): 3 6
Enter Height and Width of your rectangle #1 (h, w): 8 4
Enter the type of an element you'd like to create (r, c):c
Enter center coordinates of your c #2 (x, y): 5 1
Enter radius of circle #2 : 6

ShapeArray contains 2 elements

Element #1 is a rectangle
Center Coordinates are: (3.0, 6.0)
Height is 4.0 and Width is 8.0

Element #2 is a Circle
Center coordinates are: (5.0, 1.0)
Radius: 6.0
Circumference: 37.68
Area: 113.03999999999


Attempt at AlexanderTest with array lists based on the first one with arrays

Uses 2 array lists, one for characters and one for doubles
https://pastebin.com/nMac1Y07


I am not sure what specifically is causing an index out of bounds error to happen when I am typing in my numbers for the rectangle and circle.




Without the IndexOutOfBounds Exception I was getting this output

How many objects you'd like to create? 1
Enter the type of an element you'd like to create (r, c): r
Enter center coordinates of your r #1 (x, y): 4 6
Enter Height and Width of your rectangle #1 (h, w): 2 3

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at CharDoubleTest.main(CharDoubleTest.java:51)



This is the output I am getting for it after adding try/catch is:

How many objects you'd like to create? 2
Enter the type of an element you'd like to create (r, c): r
Enter center coordinates of your r #1 (x, y): 3 4
Enter Height and Width of your rectangle #1 (h, w): 5 7

The Index is out of bounds (try/catch exception)

Enter the type of an element you'd like to create (r, c): c
Enter center coordinates of your c #2 (x, y): 2 5
Enter radius of circle #2 : 3

The array contains 2 elements

Element #1 is a r

Element #2 is a c

The array contains 7 elements

Element #1 is a 3.0

Element #2 is a 4.0

Element #3 is a 5.0

Element #4 is a 7.0

Element #5 is a 2.0

Element #6 is a 5.0

Element #7 is a 3.0


It is not outputting anything from the rectangle2D or circle class such as the width, height, x, y values for the rectangle and the radius for the circle like in my AlexanderTest (array) class where the program works correctly.

What I have tried:

I have tried to fix the error on my own but I am not sure what exactly is causing the problem to happen. I would really like some help figuring this out please.
Posted
Updated 1-Jul-22 16:55pm
Comments
FreedMalloc 1-Jul-22 23:11pm    
Just a quick comment on the format of your question. I would have found it far easier to craft the solution I posted below if your question would have included your actual code instead of links to it. There isn't all that much code and putting it in code blocks would have kept it separate from your question text and test data. As it is I was able to view and understand your code but it was inconvenient and I nearly skipped over this question for that reason. In any case, I hope I helped you out and good luck with your changes!
Patrice T 1-Jul-22 23:36pm    
You have a secret error in your secret code.

1 solution

In your array example you create an array of Alexander_Shape objects. For each shape you enter its type and its properties. Next you create the new shape object and add it to your array.

Do the same with the ArrayList:
ArrayList<Alexander_Shape> listShape = new ArrayList<Alexander_Shape>(numObjects);


From then on create shapes as you have been and add them to listShape.
To dump them out, get each shape and output it as you have been with your normal array.
In doing it this way you will see that there is actually very little code to change to replace your array with an ArrayList.



BTW, I suspect the exception happens here. Above you added 4 doubles to your ArrayList. ArrayList.get() method takes an index as a parameter you are passing the coordinate cast to an int. The coordinate entered was 4 and your ArrayList only has 4 items at indices 0, 1, 2, 3 - 4 is out of bounds.

Alexander_Rectangle2D newShape = new Alexander_Rectangle2D(listDouble.get((int) x),
        listDouble.get((int) y),
        listDouble.get((int) h), listDouble.get((int) w));


But, if you create a single ArrayList of Alexander_shape objects as I indicated above you will bypass all this and your exception will go away.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900