So far, in a program that I am writing, I have been using arrays to store data about line segments. For the sake of the question, let's assume these arrays only contain integers that define start (x1, y1) and end (x2, y2) coordinates.
Integer[] lineData = {x1, y1, x2, y2};
In my program, I need to continuously perform operations on the data contained in multiple such arrays using for loops.
In the course of writing the program, I have, numerous times, realized that I need more data about those segments. As a result, I added elements to the array, which grew larger, for instance:
Integer[] lineData = {x1, y1, x2, y2, slope, red, green, blue, width};
This has become hard to manage, since I need to remember the position of each integer data to perform operations on it and it is very tedious to implement changes to the array, such as swapping the position of two elements, since I need to update the index of the elements in every part of the program that performs operations on them.
This has led me to the probably obvious idea to create a lineData class which contains the integers as its fields:
public class LineData {
public int x1,y1,x2,y2,slope, red, green, blue, width;
LineData(int x1, int y1, int x2, int y2, int slope, int red, int green, int blue, int width){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.slope = slope;
this.red = red;
this.green = green;
this.blue = blue;
this.width = width;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public int getSlope() {
return slope;
}
public void setSlope(int slope) {
this.slope = slope;
}
public int getRed() {
return red;
}
public void setRed(int red) {
this.red = red;
}
public int getGreen() {
return green;
}
public void setGreen(int green) {
this.green = green;
}
public int getBlue() {
return blue;
}
public void setBlue(int blue) {
this.blue = blue;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
This looks like a good solution.
My concern, though, is that accessing and changing the fields of the lineData class will be slower than accessing and changing the elements in the array. The reason behind my concern is the belief that Arrays exist for a reason.
Another argument for using a lineList class is the need to store data about the line segments that are not integers, but Strings, Booleans and other custom objects.
However, please disregard that argument. I would like to compare an integer array with a class containing solely integer fields.
Summing up, my questions are:
Which one is faster: an array of x integers or an object containing x integer fields? How did you solve the described issue when you faced it in the past? If using an object with fields is slower than using an array, did you still decide to use an object with fields? Why?
Please take into account that there is a very high number of those arrays, so changing the arrays to objects with fields would create a very high number of instantiated objects that are looped over - maybe this has an effect on performance?
Thanks a lot in advance for your help!