ArrayList not adding objects? - java

I have a piece of code:
public void setBoardMemberPosition() {
int[] position = null;
do{
positionX = RandomNumberGenerator.generateRandomNumber(Board.DIMENSION);
positionY = RandomNumberGenerator.generateRandomNumber(Board.DIMENSION);
position = new int[] { positionX, positionY };
} while(checkIfPositionIsOccupied(position));
board.setElementAt(positionX, positionY, name);
storedMembers.add(position);
}
Can someone explain to me why the elements are not being added to the list, where:
private ArrayList<int[]> storedMembers = new ArrayList<int[]>();is a Class variable and:checkIfPositionIsOccupied(position) returns true/false
if (storedMembers.contains(position))
i have a feeling its something to do with the do while, ive debugged it with no luck.

If you want to add the array position to the ArrayList on each iteration, your add() call must be inside the loop:
do {
// ...
storedMembers.add(position);
} while(...);
Edit:
for (int i = 0; i < storedMembers.size(); i ++)
// ...
if (!checkIfPositionIsOccupied(position))
storedMembers.add(position);
}

Because you don't add in your while loop anything to the list?!

Related

Problems understanding constructors like the one in ArrayList.class

While coding I was trying to declare a class that can create an arraylist of arraylists, but soon enough I found it hard to define a proper constructor for my class. I wanted to define some methods for me to handle the huge outer arraylist(1000*1000), but I might be affected by C and always tried to use something like structdef.
How should I define my class? I guess declaring every lines seperatedly is not a wise choice, and I don't want to use 2D arraylist directly. Besides, how should I define a constructor to get an object that is an 2D arraylist?
//Update here
Below is my code example:
class farbicMap {
//attribute
ArrayList<Integer> farbicUnit = new ArrayList<Integer>();
//constructor
farbicMap () {
for (int i=0;i<1000;++i) {
farbicUnit.add(0);
}//this gives an arraylist with size of 100
//I want to use the above arraylist to construct another list here
}
//method
setUnitValue(int v) {
...
}
}
Seems that I didn't really understand the concept of class... I wanted to use the class to represent a map with some nodes. Now that's much clearer to me.
This is how I understood your consern:
class Test {
public static void main(String[] args) {
Board board = new Board(1000, 1000);
board.put(1, 2, "X");
Object x = board.get(1, 2);
System.out.println("x = " + x);
}
}
class Board {
private final int xSize;
private final int ySize;
private ArrayList<ArrayList<Object>> board = new ArrayList<>();
public Board(int xSize, int ySize) {
this.xSize = xSize;
this.ySize = ySize;
for (int i = 0; i < xSize; i++) {
board.add(getListOfNulls());
}
}
public Object get(int x, int y) {
return board.get(x).get(y);
}
public void put(int x, int y, Object toAdd) {
List<Object> xs = board.get(x);
if (xs == null) {
xs = getListOfNulls();
}
xs.add(y, toAdd);
}
private ArrayList<Object> getListOfNulls() {
ArrayList<Object> ys = new ArrayList<>();
for (int j = 0; j < ySize; j++) {
ys.add(null);
}
return ys;
}
}
You should use Array if size is fixed.

Java: How to use functions of a class which has been instantiated by ArrayList

Here is just a simple example. Obviously there are simpler ways to set everything up within the constructor, but the arrayList I'm actually working with has already been set up, I just need to change individual sections of it. There HAS to be a way to call a class's functions in ArrayList, but for the life of me I can't figure out how.
import java.util.ArrayList;
public class ArrayTest{
public static void main(String[] args){
//Here's an example of a regular array:
Length[] lArray = new Length[3];
for (int i = 0; i < 3; i++){
lArray[i].setLength(i + 1);
}
//Here's how I was hoping ArrayList would function:
ArrayList<Length> lList = new ArrayList<Length>(3);
for (int i = 0; i < 3; i++){
lList[i].setLength(i + 1);
// --OR--
lList.setLength(i, i + 1);
}
}
}
Here's the length class:
public class Length{
private int length;
Length(){
length = 0;
}
Length(int s){
length = s;
}
public void setLength(int s){
length = s;
}
}
Thanks!
You add elements to the ArrayList with add.
Since it's an ArrayList<Length>, you add Length objects:
lList.add(new Length());
And in your specific loop :
ArrayList<Length> lList = new ArrayList<Length>(3);
for (int i = 0; i < 3; i++){
Length l = new Length();
l.setLength(i+1);
lList.add(l);
}
BTW, the array initialization is also missing an important initialization :
for (int i = 0; i < 3; i++){
lArray[i] = new Length(); // added
lArray[i].setLength(i + 1);
}
If the ArrayList already contains the elements, and you just want to modify them, you can write something like this:
lList.get(i).setLength(i + 1);
assuming that the ArrayList contains the ith element.
You could create a method with your operation/algorithm like
public void foo(){
System.out.println("some algorithm!");
}
inside Length class. This will operate on each instance of Length class.
And for iterating, you can use
ArrayList<Length> lList = new ArrayList<Length>(3);
for (Length l : lList){
l.foo();
}
This will call everything you code inside foo.

Array inside an Array

I'm making a program to get the midpoint of a line. However i'm running into a problem.
What i need to be able to do is have as many points as i want, and dimensions. However i have no idea how to do this
So let me give you an idea of what i want to do.
points[point][x]=1; points[point][y]=2; points[point][z]=3;
See what i'm getting at? This is what i currently have
public float[][] points={{}};
And when i want to write to it
for(int i=0; i<parts.length;i++){
points[currentPoint][i]=Float.valueOf(parts[i]);
}
java.lang.ArrayIndexOutOfBoundsException: 0
So how can i do this?
public float[][] points={{}};
This creates an empty two dimensional array. Hence, the ArrayIndexOutOfBoundsException.
If you want to create an array to hold 6 points and 3 dimensions then you have to allocate it:
public float[][] points = new float[6][3];
However, I would reconsider your design. If you would like to go more object oriented way than you would probably like to have a class for line as well as for point. For example:
class Line {
private final Point startPoint;
private final Point endPoint;
Line(Point startPoint, Point endPoint) {
this.startPoint = startPoint;
this.endPoint = endPoint;
final int startPointDimensions = startPoint.getDimensions().size();
final int endPointDimensions = endPoint.getDimensions().size();
if (startPointDimensions != endPointDimensions) {
throw new RuntimeException("Points that form a line must have the same dimension! " +
"Start point dimension: " + startPointDimensions +
"End point dimension: " + endPointDimensions);
}
}
public Point midpoint() {
// your code goes here
}
}
class Point {
private final List<Float> dimensions;
Point(List<Float> dimension) {
dimensions = dimension;
}
public List<Float> getDimensions() {
return dimensions;
}
}
You can read more about lists in the oracle's documentation. I would prefer lists to arrays if you do not know up front how many dimensions you want to support or if you want to support different number of dimensions.
Use this to create a 3x3 2-D array and initialize all values to 0:
public float points[][] = new float[3][3];
And this to iterate through the array and set values:
for (int j=0; j<3; j++) {
for (int i=0; i<3; i++) {
points[j][i] = 42; //Or whatever
}
}

doubling Array size if full

I have an array of objects. When the array fills up, I want to make a new array twice as large as the old one, and transfer all the elements over. I'm doing something wrong, I think its something to do with I'm not creating the correct reference to the new array. Here's my code, any help figuring this out would be appreciated.
private int DIRECTORY_SIZE = 6;
Entry [] directory = new Entry[DIRECTORY_SIZE];
private int numberOfElements = 0;
public int getNumOfElements(){
return numberOfElements;
}
public void setDirectorySize(int size){
DIRECTORY_SIZE = size;
}
public int getDirectorySize(){
return DIRECTORY_SIZE;
}
public void addEntry(String surname, String initial, String num) {
// TODO add an entry to an array, also increments numberOfElements variable tracking whats in array
if(getNumOfElements() == getDirectorySize()){ // if array is full
doubleArraySize(); // put temp values into new bigger directory array
}
int i = findFreeLocation();
directory[i] = new Entry(surname, initial, num);
numberOfElements++;
}
private void doubleArraySize(){
Entry[] temp = new Entry[DIRECTORY_SIZE]; //make new temp array same size as old one
for(int i = 0; i < DIRECTORY_SIZE ; i++){
temp[i] = directory[i]; // cycle through array putting all values into temp
// works up to here
}
setDirectorySize(DIRECTORY_SIZE*2); // double size of array
Entry[] directory = new Entry[DIRECTORY_SIZE]; // create new, double size directory array
for(int i = 0; i < temp.length ; i++){
directory[i] = temp[i];
}
}
private int findFreeLocation() {
int i;
for (i = 0; i < DIRECTORY_SIZE; i++)
{
if(directory[i] == null)
{
break;
}
}
return i;
}
In doubleArraySize() function , this is the issue :
Entry[] directory = new Entry[DIRECTORY_SIZE];
// you are not assigning it to the class attribute directory
// instead you are creating a local array directory
Make the following change :
this.directory = new Entry[DIRECTORY_SIZE];
// this will assign the newly created array to the class attribute
Note : I personally prefer to use this pointer to refer to class attributes so that it makes my code more readable, and its clear to everyone that the variable in question is a class attribute rather than local variable.
**SIZE has already double by this point. No need to multiple by 2
I remember doing something exactly like this when I was making a Vector ADT. However, I used instance variables instead of methods in my code for element number and the capacity. I definitely didn't initialize a Vector inside a method for a Vector.
setDirectorySize(DIRECTORY_SIZE*2); // double size of array
Entry[] directory = new Entry[DIRECTORY_SIZE]; // create new, double size directory array
Isn't DIRECTORY_SIZE an instance variable? Because if it is, I don't think you can initialize an object using an instance variable from the object you are overwriting.
Putting my code into your context, it would look something like this:
private void doubleDirectorySize()
{
Entry[] new_array = new Entry[new_directory_size*2];
for (int i = 0; i < directory_size; i++)
{
new_array[i]= directory[i];
}
directory= new_array;
}
This only works if directory was initialized to null, though, moving the pointer directory to the new array.

Java Object(Boolean,Integer) when add get NullPointerException

When I try to reset a list Marked I get a Null Pointer Exception.
The problem must be cause I never said what B and C is. (Boolean B, Integer C) And I don't know how to do this.
Here is a part of my code :
Marked[] marked;
//Create list marked!
public class Marked<B,C>{
public B bool;
public C comp;
}
public Graph(int N)
{
//Fill marked with false and 0
marked = new Marked[N];
for(int i=0;i<N;i++){
marked[i].bool = false;
marked[i].comp=0;
}
Creating an array of Marked doesn't actually initialize the elements in the array:
marked = new Marked[N];
for(int i = 0; i < N; i++) {
marked[i] = new Marked<Boolean, Integer>();
marked[i].bool = false;
marked[i].comp = 0;
}
The statement marked = new Marked[N]; creates a new array of Marked objects with N elements, but does not initialize them. Each element in this array would be null.
You need to manually initialize them by calling the constructor.
So, your for loop should look like this:
for(int i=0;i<N;i++) {
marked[i] = new Marked();
marked[i].bool = false;
marked[i].comp=0;
}

Categories