-
Java help, passing/filtering a array
Hello,
I wont post the whole program because it is a lot of classes, interfaces and abstract classes.
I am trying to filter a array by simply getting a value which is Z. The String representation of the whole application is
1,1,1:3,4,2:RoomA
x,y,z h,w,l name
The z parameter is suppose to filter through and only allow any Array above z > 1. It then returns the filter array.
I have done something similer but returns the full arraylist by coverting a array.
Code :
public static ArrayList<Room> rooms = new ArrayList<Room>();
public class Storey implements Buildable{
public static Room[] getAllRooms()
{
Room[] array = new Room[rooms.size()];
array = rooms.toArray(array);
return array = rooms.toArray(array);
}
}
The below code needs to filter it by making anything above Z then returning it.
Code :
public class engine implements model {
public Room[] getStoreyRooms(int storey) {
//storey = RoomReference.getZ();
Room[] array = new Room[Storey.rooms.size()];
// array = Storey.rooms.toArray(array);
array.equals(storey!=1);
{
// if storey.getLocation()
return Storey.rooms.toArray(array);
}
}
This is the class it gets it from.
Code :
public class RoomReference {
private int x;
private int y;
private int z;
private AbstractRoom abstractRoom ;
public RoomReference(int x, int y, int z) {
this.abstractRoom = abstractRoom;
this.x = x;
this.y = y;
this.z = z;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getZ()
{
return z;
}
public String toString()
{
String s = "";
s += x;
s += ",";
s += y;
s += ",";
s += z;
s += ":";
return s;
}
}
I hope this is enough , but if you need me to post more please dont hesitate to ask.
-
Re: Java help, passing/filtering a array
When you say filter, what is it you really want to do with the array?
Do you specifically want to filter an ArrayList?
When your filter matches, do you want to remove the matched items or keep only the matched items?
It is generally a poor idea to name variables single letters unless they are for counting in loops. Maybe you should rename your variables to something that has context with the situation you are in.
Please give some more details.
-
Re: Java help, passing/filtering a array
I want to get any array to match with the data z > 1, So it will return from the method.
Lets just say we have data populated in the arraylist.
1,1,1:3,4,2:RoomA
1,2,1:5,4,3:RoomB
1,1,2:2,2,2:RoomC
Ok , we have x,y,z ,height,width,length , name
Checking rooms with getStoreyRooms()
1,1,2:2,2,2:RoomC
As you see only one Room in the arraylist shows because the z value is higher then 1.
It needs to be implement in this method with the following parameters.
and yes it needs to be coverted to array from a arraylist as the last example.
It is in the model interface,which the content of the method must be on the engine, as shown below.
Code :
public class engine implements model {
public Room[] getStoreyRooms(int storey) {
}
Think of x,y,z as coordiates , if it is 2,2,2
[][][]
[][X][]
[][][]
z as mutiple level building
I simply want to keep the matches, and get rid of the rest. anything that matches higher then z>1
does this answer your question?,if more detail is require please reply
-
Re: Java help, passing/filtering a array
Quote:
Lets just say we have data populated in the arraylist.
1,1,1:3,4,2:RoomA
1,2,1:5,4,3:RoomB
1,1,2:2,2,2:RoomC
Does your ArrayList in this example contain three elements? What data type are the elements?
Are they Strings? Like this: "1,1,2:2,2,2:RoomC"
Or is 1,1,2:2,2,2:RoomC a representation of the contents of a Room object?
For the example you posted, would getStoryRooms() return an array with a single element?
A Room with the value: 1,1,2:2,2,2:RoomC
-
Re: Java help, passing/filtering a array
You should have a Room class if you don't already.
Code java:
for each room {
if z value is greater than 1 {
add room to temp list;
}
}
return temp list;
-
Re: Java help, passing/filtering a array
yes, i have a room class and yes it is a representation of Room ,
This is the AbstractRoom class it implements Room.
Under AbstractRoom are the classes that contain the values of each element used for the arraylist, such as the class RoomReference with x,y,z. class Rectangle with width height length, and this name on this class private String name is the roomsname. It connects with different classes then combines together, the linking of each class is correct since the other method getAllRooms() is successful, now i am trying to filter using the (z>1). I can attach the uml diagram if needed?
igorne the other arraylists.
Code :
public interface Room extends Buildable{
public void addExit();
public void addItem();
public String getExit();
public ExitPoint getExitList();
public String getItem();
public String getItemList();
public RoomReference getLocation();
public String getName();
public void removeExit();
public void removeItem();
public String toString();
public Object getExit(String string);
}
Code :
public abstract class AbstractRoom implements Room{
private String name;
private RoomReference location;
private ArrayList<ExitPoint> exits = new ArrayList<ExitPoint>();
private ArrayList<Item> items = new ArrayList<Item>();
public AbstractRoom(RoomReference location, String name)
{
this.location = location;
this.name = name;
this.items = new ArrayList<Item>();
}
public void addExit(ExitPoint v)
{
exits.add(v);
}
public void addItem(Item v)
{
items.add(v);
}
public void clearArea()
{
exits.clear();
items.clear();
}
public ExitPoint getExit(ExitPoint v)
{
return v;
}
public ExitPoint getExitList()
{
//array list
return null;
}
public Item getItem(Item i)
{
return i;
}
public String getItemList()
{
return null;
}
public RoomReference getLocation()
{
return location;
}
public String getName()
{
return name;
}
public void removeExit()
{
}
public void removeItem()
{
}
}
-
Re: Java help, passing/filtering a array
Where is the definition of z in the definition of those two classes you just posted?
If z is not defined in the class, how can you use z's value to "filter" an instance of the class?
-
1 Attachment(s)
Re: Java help, passing/filtering a array
it runs though the RoomReference class.
so RoomReference ----> Abstractroom <---- rectangleroom
Room implements Abstractroom
I have posted the UML, to hopefully show we are on the same page. attribute not included.
I havnt uploaded the engine class or the model.
Attachment 735
How does the Z value connect?
it connects by
Code :
public AbstractRoom(RoomReference location, String name)
{
this.location = location;
this.name = name;
this.items = new ArrayList<Item>();
}
RoomReference location, has the getZ() connect to it using the contractor.
As you see it all connects, I have tested the arraylist and it does successfully get the Z value using the toString() on each class. But once it is on its String representation, I cant slice it out to test the specify Z value. It shows the whole arraylist entire which i cant do (z>1);
a Element
1,1,2:2,2,2:RoomC
1,1,2
RoomReference
:2,2,2
Rectangleroom
:RoomC
Name
which the classes are called by the engine class, then to the facade class.
-
Re: Java help, passing/filtering a array
Quote:
But once it is on its String representation, I cant slice it out to test the specify Z value
Why are you trying to work with the String representation of the class? You should be working with the Room objects.
-
Re: Java help, passing/filtering a array
There is no direct way to remove elements from an array, because its size is fixed. Whatever you do, you need to allocate a new array somehow.
-
Re: Java help, passing/filtering a array
I have created a new array on the method , as shown. String representation is needed to connect to other classes like roomReference by using the toString method.
Below is code I done. r is for RoomReference I created a instance of the object in the contractor, i can only do this without making it static.
Code :
public Room[] getStoreyRooms(int storey) {
storey = r.getZ();
Room[] a = new Room[s.rooms.size()];
System.out.println(a);
if(1 > storey)
{
return a;
}
return null;
}
This line gives me a nullpointerexception
storey = r.getZ();
I would assume it would be able to get the Z value from the RoomReference class? and not give me a null.
-
Re: Java help, passing/filtering a array
Quote:
This line gives me a nullpointerexception
storey = r.getZ();
Does the r variable have a valid value or is its value null?
Where do you give the r variable the address of a Room object?
-
Re: Java help, passing/filtering a array
r is for RoomReference it reads the methond on the class that has getZ();
Code :
public engine(String name,int budget)
{
s = new Storey();
o = new Owner(name, budget, budget);
h = new House();
RoomReference r = new RoomReference(1, 1, 1);
this.name = name;
this.budget = budget;
}
-
Re: Java help, passing/filtering a array
The r variable in the engine constructor is local to the constructor. You need to move its definition outside of the constructor.
-
Re: Java help, passing/filtering a array
so to a different class?
I have notice it reads the local constructor but i dont know how i would implement it to read the contractor with the data it needs, i believe the method is corrected now. i need to find a way to access the data instead of the local data in the class. just give me a bit to try something out, please dont lock this thread.
-
Re: Java help, passing/filtering a array
Code :
RoomReference r = null; // define the variable here
public engine(String name,int budget)
{
s = new Storey();
o = new Owner(name, budget, budget);
h = new House();
r = new RoomReference(1, 1, 1); // give it a value here
this.name = name;
this.budget = budget;
}
-
Re: Java help, passing/filtering a array
I have tried the above no sucess, it still gives me a null exception.
-
Re: Java help, passing/filtering a array
Quote:
it still gives me a null exception.
Please copy and paste the FULL text of the error message.
There are many ways for r to be null.
Are you sure that this statement:
r = new RoomReference(1, 1, 1)
is executed BEFORE you try to use the value of r?
Is there more than one r variable? And you are using one that does not have a value.
-
Re: Java help, passing/filtering a array
Code :
public Room[] getStoreyRooms(int storey) {
storey = r.getZ();
Room[] a = new Room[Storey.rooms.size()];
System.out.println(Storey.rooms);
if(1 < storey)
{
return a;
}
return null;
}
Code :
RoomReference r = null;
public HomeCADengine(String name,int budget)
{
s = new Storey();
o = new Owner(name, budget, budget);
h = new House();
RoomReference r = new RoomReference(1, 1, 1);
this.name = name;
this.budget = budget;
}
I have tried the above no sucess, it still gives me a null exception, I am using the r value.
Error as shown below:
at model.facade.engine.getStoreyRooms(engine.java:62)
at test.Test.testAddRoom(Test.java:141)
at test.Test.main(Test.java:1157)
The Test.java file is definately correctly.
-
Re: Java help, passing/filtering a array
Where do you give r a value? Your code does not show where the r variable that is used in getStoreyRooms() is gettting a value.
You have the same problem in this code that you had before. You have defined a local variable with the same name as the class variable. That definition hides the class variable and does not change its value:
RoomReference r = new RoomReference(1, 1, 1);
Remove the RoomReference and leave it like this:
r = new RoomReference(1, 1, 1);
-
Re: Java help, passing/filtering a array
ok the new output is shown, as you see it fails to filter by only showing 1,1,2:2,2,2:RoomC::
Code :
Checking rooms with getStoreyRooms()...[1,1,1:3,4,2:RoomA::, 1,2,1:5,4,3:RoomB::, 1,1,2:2,2,2:RoomC::]
Parsing error: could not check room list successfully!
This is the line it gives bad output, on the test.java
Code :
String[] expected = { "1,1,1:3,4,2:RoomA::", "1,2,1:5,4,3:RoomB::",
"1,1,2:2,2,2:RoomC::" };
String[] expected1 = { "1,1,2:2,2,2:RoomC::" };
if (failureCheck(checkForRooms(model.getStoreyRooms(2), expected1),
true))
return false;
It shows the data on the arraylist but fails to filter it. the above (2) is expected to be the output. but it doesnt it shows all 3 arraylist entires.
-
Re: Java help, passing/filtering a array
What do you expect anyone to recommend from this little post?
Is this what you are trying to do>
You have an array list containing Room objects.
You have a loop that goes through the objects in the array list and selects some based on the object's z value.
The selected objects are to be removed from the original arraylist and added to a new array list.
You then display the contents of the new arraylist.
The problem is that an incorrect object is selected and moved to the new arraylist.
The solution is to check the logic for making the selection. Why does it select an object with the incorrect z value?