How would I implement my own method on an element of my own array?
for example, im trying to make a inventory system that if (INVENTORY[X].isValidID() == TRUE)
Then it returns true,
how would i do that?
public void addInv(int ITEM_ID) {
for(int x = 0; x < MAX_STORAGE; x++) {
if(INVENTORY[x].isValidID() == true ) {
}
}
}
public static boolean isValidID(int X) {
return true;
}
You are likely looking for something like this.
public void addInv(int ITEM_ID) {
for(int x = 0; x < MAX_STORAGE; x++) {
if(isValidID(INVENTORY[x])) {
}
}
}
public static boolean isValidID(int X) {
return true;
}
Related
This question already has answers here:
How to read the value of a private field from a different class in Java?
(14 answers)
Closed 4 years ago.
So guys im trying to access a private Array from another class. Is there a way to access said array without a get-Method for the array?
public class Entity {
private int key;
private int value;
public Entity(int k, int v) {
key = k;
value = v;
}
public int getKey() {
return key;
}
public int getValue() {
return value;
}
public void setValue(int v) {
value = v;
}
public void setKey(int k) // selbst geadded
{
key = k;
}
}
Those are the elements that are contained in the array.
public class Relation {
private Entity[] map;
public Relation(int n) {
map = new Entity[n]; // größe des neuen feldes
}
public int size() {
return map.length;
}
public Entity extract(int i) {
if (i >= map.length || i < 0 || map[i] != null) {
return null;
}
int key = map[i].getKey();
int value = map[i].getValue();
map[i] = null;
return new Entity(key, value);
}
public boolean into(Entity e) {
for (int i = 0; i < size(); i++) {
if (map[i] == null) {
map[i] = e;
return true;
}
}
return false;
}
public static void main(String[] args) {
}
}
Relation is the Method im supposed to use. This class contains the private array which im trying to access.
public class Use {
public static boolean substitute(Relation rel, Entity e) {
if (rel.size() > 0) {
rel.map[0] = e; // "map has private acccess in Relation"
return true;
}
return false;
}
public static Relation eliminate(Relation rel, int k) {
int counter = 0;
for (int i = 0; i < rel.size(); i++) {
if (map[i] != k) // // "cannot find symbol map"
{
counter++;
}
}
}
}
And this is the class in which im trying to access the array. The methods here are not finished yet since im getting errors whenever im trying to access the map in the Relation class in any why since I cant figure it out.
To access fields, you need a FieldInfo:
Type relationType = typeof(Relation);
FieldInfo fieldRelationMap = relationType.GetField("map",
BindingFlags.Instance | BindingFlags.NonPublic);
FieldInfo has a GetValue and a SetValue
I am trying to fill UCFCourse courseOne in my constructor with a courses[] object in fillWithCourses().UCFCourse courseOne does populate outside of the constructor but will not go into it.
public class UCFSemester<courses> {
private static UCFCourse courseOne;
private static double totalSemesters;
private static double completionTime;
static boolean fillSemester = true;
public UCFSemester(UCFCourse courseOne, UCFCourse[] coursetwo) {
this.courseOne = courseOne;
}
public static UCFCourse getcourseOne() {
return courseOne;
}
public static void setCoursesone(UCFCourse courses) {
courseOne = courses;
}
public static void fillWithCourses(UCFCourse courses[], int l) {
int x = 0;
while (fillSemester) {
for (int n = 0; n < 5; n++) {
if (x != n && courses[x].getCourseLevel() < courses[n].getCourseLevel()) {
setCoursesone(courses[x]);
}
}
fillSemester = false;
}
}
}
Side question.How can I access this all in a non-static way?I need the entire thing to be non-static but no matter what I do I can't get it.Thanks!
You can simply do it by creating a List like this:
public class UCFSemester {
private List<UCFCourse> courseList = new ArrayList<>();
public UCFCourse getCourse(int index) {
return courseList.get(index);
}
public void addCourses(UCFCourse[] courses) {
for(int x = 0; x < courses.length; x++) {
courseList.add(courses[x]);
}
}
}
Here, I'm assuming that you are passing the UCFCourse[] array with all the course details that are there in that particular semester.
addCourses() function will take this array and then add all the corresponding courses to the List.
getCourse() function will return you any particular course from the List (Using Index). You can also modify the search in any way you want.
I am not good with arrays.In the below code there are 2 methods which takes the data from two 2D Arrays.The first method COMBINATION is working correctly.But the second Method doesn't.
Am I doing anything wrong in the array Declaration ???
The Below Methods takes the KeyEvent from the Array & perform the Keypress & keyRelease according to the KeyEvents in the array.
For Example :
Combination.UNICODE_COPY, KeyEvent.VK_CONTROL, KeyEvent.VK_C
The above line presses CTRL+C. It performs the Copy Function.
I think that in the Second Method i am passing 3 KeyEvents.Is that a Problem ???
private static final int[][] COMBINATION = {
{
Combination.UNICODE_CUT, KeyEvent.VK_CONTROL, KeyEvent.VK_X
}, {
Combination.UNICODE_COPY, KeyEvent.VK_CONTROL, KeyEvent.VK_C
}, {
Combination.UNICODE_PASTE, KeyEvent.VK_CONTROL, KeyEvent.VK_V
}
};
private static final int[][] COMBINATIONS = {
{
Combinations.UNICODE_TASK, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_DELETE
}
};
private void keyboardCombination(Combination action)
{
boolean exception = false;
for (int i = 0; i < COMBINATION.length; i++)
{
if (action.unicode == COMBINATION[i][0])
{
exception = true;
this.application.getRobot().keyPress(COMBINATION[i][1]);
this.application.getRobot().keyPress(COMBINATION[i][2]);
// this.application.getRobot().keyPress(COMBINATION[i][3]);
this.application.getRobot().keyRelease(COMBINATION[i][1]);
this.application.getRobot().keyRelease(COMBINATION[i][2]);
// this.application.getRobot().keyRelease(COMBINATION[i][3]);
break;
}
}
if (!exception)
{
pressUnicode(this.application.getRobot(), action.unicode);
}
}
private void keyboardCombinations(Combinations action)
{
boolean exception = false;
for (int i = 0; i < COMBINATIONS.length; i++)
{
if (action.unicode == COMBINATIONS[i][0])
{
exception = true;
this.application.getRobot().keyPress(COMBINATIONS[i][1]);
this.application.getRobot().keyPress(COMBINATIONS[i][2]);
this.application.getRobot().keyPress(COMBINATIONS[i][3]);
this.application.getRobot().keyRelease(COMBINATIONS[i][1]);
this.application.getRobot().keyRelease(COMBINATIONS[i][2]);
this.application.getRobot().keyRelease(COMBINATIONS[i][3]);
break;
}
}
if (!exception)
{
pressUnicode(this.application.getRobot(), action.unicode);
}
}
COMBINATIONS
public class Combinations extends ControllerDroidAction
{
public static final int UNICODE_TASK = 40;
public int unicode;
public Combinations(int unicode)
{
this.unicode = unicode;
}
public static Combinations parse(DataInputStream dis) throws IOException
{
int unicode = dis.readInt();
return new Combinations(unicode);
}
public void toDataOutputStream(DataOutputStream dos) throws IOException
{
dos.writeByte(COMBINATIONS);
dos.writeInt(this.unicode);
}
}
For a lot of implementation reasons (Using Java ME 1.4 with very limited libraries), I have no access to HashMap or any kind of Map interface. In combination with this, I have to use Hashtable, which in the libraries I'm using does not inherit anything.
Yes, there is absolutely no way for me to get around the implementation and libraries that I'm using.
So I have two Hashtables. I need to make one new Hashtable instance that accesses and changes the two "backing" instances. Since Hashtable does not inherit anything, is there any way I could do this? I've tried a rudimentary composing strategy that just goes through an array of tables, but there are some serious problems with that. Specifically, put(key, object) is difficult because there's no way to tell which map it is being backed to.
Any suggestions on a strategy to do this or am I stuck?
public class Scope {
private final Hashtable publicVars;
private final Hashtable publicMethods;
private final Hashtable publicReturning;
private final Hashtable privateVars;
private final Hashtable privateMethods;
public Scope() {
publicMethods = new Hashtable();
publicReturning = new Hashtable(0);
publicVars = new Hashtable();
privateVars = new Hashtable();
privateMethods = new Hashtable();
}
public Scope(Scope scope) {
publicVars = scope.publicVars;
publicMethods = scope.publicMethods;
publicReturning = scope.publicReturning;
privateVars = new Hashtable();
privateMethods = new Hashtable();
// Here's my problem - I need changes made to publicVars to also affect scope.privateVars (and the same to methods)
publicVars.putAll(scope.privateVars);
publicMethods.putAll(scope.privateMethods);
}
private static final class MapGroup {
private final List maps = new ArrayList();
public MapGroup(Hashtable start) {
maps.add(start);
}
public MapGroup(MapGroup group) {
for (int x = 0; x < group.maps.size(); x++) {
maps.add(group.maps.get(x));
}
}
public void add(Hashtable h) {
maps.add(h);
}
public Enumeration keys() {
return new Enumeration() {
private final Enumeration[] enumerations;
private int i;
{
enumerations = new Enumeration[maps.size()];
for (int x = 0; x < maps.size(); x++) {
enumerations[x] = ((Hashtable) maps.get(x)).keys();
}
}
public boolean hasMoreElements() {
return enumerations[i].hasMoreElements()
|| (++i < enumerations.length && enumerations[i].hasMoreElements());
}
public Object nextElement() {
// needed to increment i
return hasMoreElements() ? enumerations[i].nextElement() : null;
}
};
}
public Enumeration elements() {
return new Enumeration() {
private final Enumeration[] enumerations;
private int i;
{
enumerations = new Enumeration[maps.size()];
for (int x = 0; x < maps.size(); x++) {
enumerations[x] = ((Hashtable) maps.get(x)).elements();
}
}
public boolean hasMoreElements() {
return enumerations[i].hasMoreElements()
|| (++i < enumerations.length && enumerations[i].hasMoreElements());
}
public Object nextElement() {
// needed to increment i
return hasMoreElements() ? enumerations[i].nextElement() : null;
}
};
}
public boolean contains(Object value) {
for (int x = 0; x < maps.size(); x++) {
if (((Hashtable) maps.get(x)).contains(value)) {
return true;
}
}
return false;
}
public boolean containsKey(Object key) {
for (int x = 0; x < maps.size(); x++) {
if (((Hashtable) maps.get(x)).containsKey(key)) {
return true;
}
}
return false;
}
public Object get(Object key) {
for (int x = 0; x < maps.size(); x++) {
if (((Hashtable) maps.get(x)).containsKey(key)) {
return ((Hashtable) maps.get(x)).get(key);
}
}
return null;
}
public Object put(Object key, Object value) {
for (int x = 0; x < maps.size(); x++) {
if (((Hashtable) maps.get(x)).containsKey(key)) {
return ((Hashtable) maps.get(x)).put(key, value);
}
}
return ((Hashtable) maps.get(maps.size() - 1)).put(key, value);
}
public Object remove(Object key) {
// Nothing is ever removed - don't worry
return null;
}
public void clear() {
}
public int size() {
int s = 0;
for (int x = 0; x < maps.size(); x++) {
s += ((Hashtable) maps.get(x)).size();
}
return s;
}
public boolean isEmpty() {
return size() == 0;
}
}
Thanks for making me have to think about this guys. I wrote this and it works for me.
My binary tree looks pretty close to my class materials, but when I print to the console or check for contains(), any adds I'm doing aren't registered.
I don't have a great understanding of static and the debugger is giving me a hint about making a static reference to non-static variable overallRoot, but everything compiles without error or warning in eclipse.
public class BSTSimpleSet<E extends Comparable<E>> implements SimpleSet<E> {
private GTNode<E> overallRoot;
private int size;
public static void main(String[] args) {
BSTSimpleSet<Integer> main = new BSTSimpleSet<Integer>(2);
main.toString();
main.add(3);
main.toString();
main.add(4);
main.toString();
main.add(5);
main.toString();
System.out.print(main.contains(3));
}
public BSTSimpleSet() {
size = 0;
}
public BSTSimpleSet(E input) {
overallRoot = new GTNode<E>(input);
size = 1;
}
public boolean add(E e) {
return add(e, overallRoot);
}
private boolean add(E e, GTNode<E> root) {
if (root == null) {
root = new GTNode<E>(e);
size++;
return true;
} else {
int compare = e.compareTo(root.data);
if (compare == 0) {
return false;
} else if (compare < 0) {
return add(e, root.left);
} else {
return add(e, root.right);
}
}
}
public void clear() {
overallRoot = null;
}
public boolean contains(E e) {
return contains(e, overallRoot);
}
private boolean contains(E e, GTNode<E> root) {
if (root == null) {
return false;
} else {
int compare = e.compareTo(root.data);
if (compare == 0) {
return true;
} else if (compare < 0) {
return contains(e, root.left);
} else {
return contains(e, root.right);
}
}
}
public boolean isEmpty() {
if (overallRoot == null) {
return false;
} else {
return true;
}
}
public int size() {
return size;
}
public String toString() {
this.toString(overallRoot, 0);
return null;
}
private void toString(GTNode<E> root, int level) {
if (root != null) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(root.data);
toString(root.left, level + 1);
toString(root.right, level + 1);
} else {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println("_");
}
}
private static class GTNode<E extends Comparable<E>> {
public E data;
public GTNode<E> left;
public GTNode<E> right;
public GTNode(E input) {
this(input, null, null);
}
public GTNode(E input, GTNode<E> lNode, GTNode<E> rNode) {
data = input;
left = lNode;
right = rNode;
}
}
}
This code does absolutely nothing.
private boolean add(E e, GTNode<E> root) {
if (root == null) {
root = new GTNode<E>(e);
size++;
return true;
}
...
Java passes in the Object Reference to a method. If you change the Reference, that will not
be propagated back to the calling method. If you change what the Reference refers to
that will be propagated back.
eg
// arrays behave the same way so using them to illustrate.
public void callMethods(){
int[] array = new int[1];
array[0] = 0;
doesNotChange(array);
System.out.println(array[0]);// will print 0
doesAChange(array);
System.out.println(array[0]);// will print 1
}
public void doesNotChange(int[] myArray){
myArray = new int[1];
myArray[0] = 1;
}
public void doesAChange(int[] myArray){
myArray[0] = 1;
}
To avoid these sorts of things I recommend always setting method parameters final.
The GTNode class shouldn't be static. Static classes are classes with only static methods, which means they don't have to be instantiated. The prototypical example of this is the java.lang.Math class: You don't need to call something like Math m = new Math(); m.cos(); to get the cosine, you just call Math.cos(). Since you're creating multiple instances of the GTNode class, make it non-static and you should be good.