I am trying to set up a SAX Handler to parse XML with the following structure:
<Hours>
<Set name="BUSINESS">
<MO>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</MO>
<TU>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</TU>
<WE>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</WE>
<TH>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</TH>
<FR>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</FR>
<SA/>
<SU/>
</Set>
<Set name="LASTCOLLECTION">
<MO>
<Close>17:00:00</Close>
</MO>
<TU>
<Close>17:00:00</Close>
</TU>
<WE>
<Close>17:00:00</Close>
</WE>
<TH>
<Close>17:00:00</Close>
</TH>
<FR>
<Close>17:00:00</Close>
</FR>
<SA/>
<SU/>
</Set>
</Hours>
I am running into some problems, however. My code will only really get the value of OPEN and CLOSE in MO, not in any of the other days of the week. Can anyone find a flaw in my logic? Here is my handler code:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"BUSINESS")) {
inBusiness = true;
} else if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"LASTCOLLECTION")) {
inLastCollection = true;
} else if (localName.equalsIgnoreCase("MO")) {
inMonday = true;
} else if (localName.equalsIgnoreCase("TU")) {
inTuesday = true;
} else if (localName.equalsIgnoreCase("WE")) {
inWednesday = true;
} else if (localName.equalsIgnoreCase("TH")) {
inThursday = true;
} else if (localName.equalsIgnoreCase("FR")) {
inFriday = true;
} else if (localName.equalsIgnoreCase("SA")) {
inSaturday = true;
} else if (localName.equalsIgnoreCase("SU")) {
inSunday = true;
} else if (localName.equalsIgnoreCase("Open")) {
inOpen = true;
} else if (localName.equalsIgnoreCase("Close")) {
inClose = true;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
value = value + new String(ch, start, length).trim();
}
public void endElement(String uri, String name, String qName)
throws SAXException {
if (inBusiness) {
bHours = new BusinessHours();
if (inMonday) {
if (inOpen) {
bHours.setMondayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setMondayClose(value);
inClose = false;
}
inMonday = false;
} else if (inTuesday) {
if (inOpen) {
bHours.setTuesdayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setTuesdayClose(value);
inClose = false;
}
inTuesday = false;
} else if (inWednesday) {
if (inOpen) {
bHours.setWednesdayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setWednesdayClose(value);
inClose = false;
}
inWednesday = false;
} else if (inThursday) {
if (inOpen) {
bHours.setThursdayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setThursdayClose(value);
inClose = false;
}
inThursday = false;
} else if (inFriday) {
if (inOpen) {
bHours.setFridayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setFridayClose(value);
inClose = false;
}
inFriday = false;
} else if (inSaturday) {
if (inOpen) {
bHours.setSaturdayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setSaturdayClose(value);
inClose = false;
}
inSaturday = false;
} else if (inSunday) {
if (inOpen) {
bHours.setSundayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setSundayClose(value);
inClose = false;
}
inSunday = false;
}
myLoc.setBusinessHours(bHours);
inBusiness = false;
} else if (inLastCollection) {
cHours = new LastCollectionHours();
if (inMonday) {
if (inOpen) {
cHours.setMondayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setMondayClose(value);
inClose = false;
}
inMonday = false;
} else if (inTuesday) {
if (inOpen) {
cHours.setTuesdayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setTuesdayClose(value);
inClose = false;
}
inTuesday = false;
} else if (inWednesday) {
if (inOpen) {
cHours.setWednesdayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setWednesdayClose(value);
inClose = false;
}
inWednesday = false;
} else if (inThursday) {
if (inOpen) {
cHours.setThursdayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setThursdayClose(value);
inClose = false;
}
inThursday = false;
} else if (inFriday) {
if (inOpen) {
cHours.setFridayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setFridayClose(value);
inClose = false;
}
inFriday = false;
} else if (inSaturday) {
if (inOpen) {
cHours.setSaturdayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setSaturdayClose(value);
inClose = false;
}
inSaturday = false;
} else if (inSunday) {
if (inOpen) {
cHours.setSundayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setSundayClose(value);
inClose = false;
}
inSunday = false;
}
myLoc.setLastCollectionHours(cHours);
inLastCollection = false;
}
}
UPDATED CODE:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
value = new String();
if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase("BUSINESS")) {
currentElement = 1;
bHours = new BusinessHours();
} else if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"LASTCOLLECTION")) {
currentElement = 2;
cHours = new LastCollectionHours();
} else if (localName.equalsIgnoreCase("MO")) {
day = 1;
} else if (localName.equalsIgnoreCase("TU")) {
day = 2;
} else if (localName.equalsIgnoreCase("WE")) {
day = 3;
} else if (localName.equalsIgnoreCase("TH")) {
day = 4;
} else if (localName.equalsIgnoreCase("FR")) {
day = 5;
} else if (localName.equalsIgnoreCase("SA")) {
day = 6;
} else if (localName.equalsIgnoreCase("SU")) {
day = 7;
} else if (localName.equalsIgnoreCase("Open")) {
open = 1;
} else if (localName.equalsIgnoreCase("Close")) {
open = 2;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
value = value + new String(ch, start, length).trim();
} // end Method Characters
public void endElement(String uri, String name, String qName)
throws SAXException {
if (name.equalsIgnoreCase("Set") && currentElement == 1) {
myLoc.setBusinessHours(bHours);
} else if (name.equalsIgnoreCase("Set") && currentElement == 2) {
myLoc.setLastCollectionHours(cHours);
}
if (currentElement == 1) {
if (day == 1) {
if (open == 1) {
bHours.setMondayOpen(value);
} else if (open == 2) {
bHours.setMondayClose(value);
}
} else if (day == 2) {
if (open == 1) {
bHours.setTuesdayOpen(value);
} else if (open == 2) {
bHours.setTuesdayClose(value);
}
} else if (day == 3) {
if (open == 1) {
bHours.setWednesdayOpen(value);
} else if (open == 2) {
bHours.setWednesdayClose(value);
}
} else if (day == 4) {
if (open == 1) {
bHours.setThursdayOpen(value);
} else if (open == 2) {
bHours.setThursdayClose(value);
}
} else if (day == 5) {
if (open == 1) {
bHours.setFridayOpen(value);
} else if (open == 2) {
bHours.setFridayClose(value);
}
} else if (day == 6) {
if (open == 1) {
bHours.setSaturdayOpen(value);
} else if (open == 2) {
bHours.setSaturdayClose(value);
}
} else if (day == 7) {
if (open == 1) {
bHours.setSundayOpen(value);
} else if (open == 2) {
bHours.setSundayClose(value);
}
}
} else if (currentElement == 2) {
if (day == 1) {
if (open == 1) {
cHours.setMondayOpen(value);
} else if (open == 2) {
cHours.setMondayClose(value);
}
} else if (day == 2) {
if (open == 1) {
cHours.setTuesdayOpen(value);
} else if (open == 2) {
cHours.setTuesdayClose(value);
}
} else if (day == 3) {
if (open == 1) {
cHours.setWednesdayOpen(value);
} else if (open == 2) {
cHours.setWednesdayClose(value);
}
} else if (day == 4) {
if (open == 1) {
cHours.setThursdayOpen(value);
} else if (open == 2) {
cHours.setThursdayClose(value);
}
} else if (day == 5) {
if (open == 1) {
cHours.setFridayOpen(value);
} else if (open == 2) {
cHours.setFridayClose(value);
}
} else if (day == 6) {
if (open == 1) {
cHours.setSaturdayOpen(value);
} else if (open == 2) {
cHours.setSaturdayClose(value);
}
} else if (day == 7) {
if (open == 1) {
cHours.setSundayOpen(value);
} else if (open == 2) {
cHours.setSundayClose(value);
}
}
}
}
Thanks!
On Line 116 You are setting inBusiness = false - which will get set after monday's open/close. Next time around it fails the if(inBusiness) on line 44.
Instead of using a inBusiness boolean, use a int indicator as follows:
private int curElement = 0;
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"BUSINESS")) {
curElement = 1;
} else if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"LASTCOLLECTION")) {
curElement = 2;
}
......
}
public void endElement(String uri, String name, String qName)
throws SAXException {
if (curElement == 1) {
....
//Remove inBusiness= false;
} else if (curElement == 2) {
...
//Remove inLastCollection = false;
}
}
Related
I have a method that tests if a binary search tree is an AVL tree, and am not getting the results i want.
here is the code :fg(left child) - fd(right child) - ag(left tree) - Noeud(node) - racine(root)
package cm5_TP_algo;
class Noeud {
int height;
int v;
Noeud fg;
Noeud fd;
Noeud(Noeud fg,int v,Noeud fd){
this.fg=fg;
this.v=v;
this.fd=fd;
}
public String toString(){
String sb = "[";
if (fg!=null)sb += fg.toString();
sb += v;
if (fd!=null)sb += fd.toString();
return sb + "]";
}
boolean testABR(int min, int max) {
if (fg != null && !fg.testABR(min, v)) {
return false;
}
if (fd != null && !fd.testABR(v, max)) {
return false;
}
return v >= min && v <= max;
}
public int calculateHeight() {
int heightLeft = (fg == null) ? 0 : fg.calculateHeight();
int heightRight = (fd == null) ? 0 : fd.calculateHeight();
height = Math.max(heightLeft, heightRight) + 1;
return height;
}
}
public class Arbre {
Noeud racine;
public Arbre(){ racine=null;}
public Arbre(Arbre ag, int v, Arbre ad) {
racine = new Noeud (ag.racine, v, ad.racine);
}
public String toString() {
if (racine == null) { return ""; }
else { return racine.toString(); }
}
public boolean testABR() {
return racine == null || racine.testABR(Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public void inser(int value){
if(racine == null)
racine = new Noeud(null, value, null);
else{
inser(value, racine);
}
}
public void inser(int value, Noeud racine){
if ( racine.v > value && racine.fg == null ) {
racine.fg = new Noeud(null, value, null);
return;
}
else if(racine.v > value && racine.fg != null){
inser(value, racine.fg);
}
if (racine.v < value && racine.fd == null ) {
racine.fd = new Noeud(null, value, null);
return;
}
else if(racine.v < value && racine.fd != null) {
inser(value, racine.fd);
}
}
public boolean membre(int value) {
if(racine == null)
return false;
if(racine.v == value)
return true;
if(racine.v < value && racine.fd != null )
return membre(value, racine.fd);
if(racine.v > value && racine.fg != null)
return membre(value, racine.fg);
return false;
}
private boolean membre(int value, Noeud racine) {
if(racine.v == value)
return true;
if(racine.v < value && racine.fd != null )
return membre(value, racine.fd);
if(racine.v > value && racine.fg != null)
return membre(value, racine.fg);
return false;
}
public void updateHeights() {
if (racine != null) {
racine.calculateHeight();
}
}
private int getHeight(Noeud noeud) {
if (noeud == null) {
return -1;
}
return noeud.height;
}
public boolean testAVL() {
updateHeights();
return checkBalanced();
}
private boolean checkBalanced() {
return checkBalanced(this.racine);
}
private boolean checkBalanced(Noeud node) {
if (node == null) return true;
int balance = getHeight(node.fd) - getHeight(node.fg);
if (Math.abs(balance) > 1) {return false;}
return checkBalanced(node.fd) && checkBalanced(node.fg);
}
public static void main(String[] args) {
Arbre t1= new Arbre( new Arbre() ,1, new Arbre() );
Arbre t2= new Arbre(
new Arbre(
new Arbre( )
, 12,
new Arbre( ))
, 13,
new Arbre() );
Arbre t3= new Arbre( t1,15,t2);
Arbre t4= new Arbre( t2,16,t3);
System.out.println(t1+":"+t1.testAVL());
System.out.println(t2+":"+t2.testAVL());
System.out.println(t3+":"+t3.testAVL());
System.out.println(t4+":"+t4.testAVL());
}
}
The expected result is :
[[12]13]:true
[115[[12]13]]:false
[[[12]13]16[115[[12]13]]]:false
But what i get is :
[[12]13]:false
[115[[12]13]]:false
[[[12]13]16[115[[12]13]]]:false
Is the problem with the testAVL or the updateHeights ?
I am learning about Binary Search Tree and working on a project. When I tried to delete the first node I inserted, it doesn't delete and still show that node when I call showAll method. Is that how Binary Search Tree works or did I do something incorrectly?
Thanks,
Here is the code for BinarySearchTree class:
public class BinarySearchTree_Pham {
TreeNode root;
public BinarySearchTree_Pham() {
root = null;
}
public boolean insert(Student_Pham newStudent) {
TreeNodeWrapper p = new TreeNodeWrapper();
TreeNodeWrapper c = new TreeNodeWrapper();
TreeNode n = new TreeNode();
if(n == null)
return false;
else {
n.node = newStudent.deepCopy();
n.lc = null;
n.rc = null;
if(root == null) {
root = n;
}
else {
findNode(newStudent.getId(), p, c);
if(newStudent.getId().compareTo(p.get().node.getId()) < 0)
p.get().lc = n;
else
p.get().rc = n;
}
return true;
}
} //end insert
public Student_Pham fetch(String id) {
boolean found;
TreeNodeWrapper p = new TreeNodeWrapper();
TreeNodeWrapper c = new TreeNodeWrapper();
found = findNode(id, p, c);
if (found == true)
return c.get().node.deepCopy();
else
return null;
} //end fetch
public boolean delete(String id) {
boolean found;
TreeNodeWrapper p = new TreeNodeWrapper();
TreeNodeWrapper c = new TreeNodeWrapper();
TreeNode largest;
TreeNode nextLargest;
found = findNode(id, p, c);
if(found == false)
return false;
else {
if(c.get().lc == null && c.get().rc == null) {
if (p.get().lc == c.get())
p.get().lc = null;
else
p.get().rc = null;
} //end case 1
else if (c.get().lc == null || c.get().rc == null) {
if (p.get().lc == c.get()) {
if (c.get().lc != null)
p.get().lc = c.get().lc;
else
p.get().lc = c.get().rc;
}
else {
if (c.get().lc != null)
p.get().rc = c.get().lc;
else
p.get().rc = c.get().rc;
}
} // end case 2
else {
nextLargest = c.get().lc;
largest = nextLargest.rc;
if (largest != null) {
while (largest.rc != null) {
nextLargest = largest;
largest = largest.rc;
}
c.get().node = largest.node;
nextLargest.rc = largest.lc;
}
else {
nextLargest.rc = c.get().rc;
if (p.get().lc == c.get())
p.get().lc =nextLargest;
else
p.get().rc = nextLargest;
}
} // end case 3
return true;
}
} // end of delete
public boolean update(String id, Student_Pham newStudent) {
if(delete(id) == false)
return false;
else if (insert(newStudent) == false)
return false;
return true;
} // end update
public void showAll() {
if (root == null)
System.out.println("Structure is empty.");
else
LNRoutputTraversal(root);
} //end showAll
private void LNRoutputTraversal(TreeNode root) {
if (root.lc != null)
LNRoutputTraversal(root.lc);
System.out.println(root.node);
if (root.rc != null)
LNRoutputTraversal(root.rc);
}
public class TreeNode {
private Student_Pham node;
private TreeNode lc;
private TreeNode rc;
public TreeNode() {}
}
private boolean findNode(String targetKey, TreeNodeWrapper parent, TreeNodeWrapper child) {
parent.set(root);
child.set(root);
if (root == null)
return true;
while (child.get() != null) {
if(child.get().node.getId().compareTo(targetKey) == 0)
return true;
else {
parent.set(child.get());
if(targetKey.compareTo(child.get().node.getId()) < 0)
child.set(child.get().lc);
else
child.set(child.get().rc);
}
} //end while
return false;
}
public class TreeNodeWrapper {
TreeNode treeRef = null;
public TreeNodeWrapper() {}
public TreeNode get() {return treeRef;}
public void set(TreeNode t) {treeRef = t;}
}
}
I have a custom hashtable implementation in java.
public class HashSet<T> implements HashTableInterface<T> {
private static int DEFAULT_ARRAY_SIZE = 10;
private T[] items;
public HashSet() {
final T[] items = (T[]) new Object[DEFAULT_ARRAY_SIZE];
this.items = items;
}
#Override
public boolean add(T item) {
int index = getIndex(item);
do {
if (items[index] != null) {
index = (index + 1) % DEFAULT_ARRAY_SIZE;
} else {
items[index] = item;
break;
}
} while (index != getIndex(item));
return true;
}
#Override
public boolean remove(T item) {
if (contains(item)) {
items[getIndex(item)] = null;
return true;
} else {
return false;
}
}
#Override
public boolean contains(T item) {
T itemArray = items[getIndex(item)];
if (item.equals(itemArray)) {
return true;
} else {
int index = (getIndex(item) + 1) % DEFAULT_ARRAY_SIZE;
do {
if (items[index] != null) {
if (items[index].equals(item)) {
return true;
} else {
index = (index + 1) % DEFAULT_ARRAY_SIZE;
}
} else {
break;
}
} while (index != getIndex(item));
}
return items[getIndex(item)] != null;
}
#Override
public int getIndex(T item) {
return item.hashCode() % DEFAULT_ARRAY_SIZE;
}
#Override
public int size() {
int count = 0;
for (T item : items) {
if (item != null) {
count++;
}
}
return count;
}
#Override
public String toString() {
return items.toString();
}}
In my add method I want to check if the place where the item would be stored is free, if not it should go to the next index. Until it finds an empty place.
My code works but I think, there could be a better way to do this.
public boolean add(T item) {
int index = getIndex(item);
do {
if (items[index] != null) {
index = (index + 1) % DEFAULT_ARRAY_SIZE;
} else {
items[index] = item;
break;
}
} while (index != getIndex(item));
return true;
}
I have the same problem in the contains method
public boolean contains(T item) {
T itemArray = items[getIndex(item)];
if (item.equals(itemArray)) {
return true;
} else {
int index = (getIndex(item) + 1) % DEFAULT_ARRAY_SIZE;
do {
if (items[index] != null) {
if (items[index].equals(item)) {
return true;
} else {
index = (index + 1) % DEFAULT_ARRAY_SIZE;
}
} else {
break;
}
} while (index != getIndex(item));
}
return items[getIndex(item)] != null;
}
There a many different ways to do collision avoidance, what you did is called "Linear Probing".
There is also (reference here)
Quadratic probing
Double hashing
And schemes that use linked lists for colliding values.
All of these have different tradeoffs which you should inform yourself on to make an informed decision.
Hey guys so I have 3 imageButton icons/tags I want to set. So I do a loop and go through them. Now the user can press a number of tags (such as a food tag, retail tag, housing tag, etc) and that adds to a Global ArrayList. Now if the user only pressed 1 tag and there's three imageButtons that need to be set, I want to set the first imagebutton to the only tag they picked and set the rest to a white blank image, but I keep getting the following error:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
here is my loop I dunno what I'm doing wrong here.
private void getIcons()
{
iconArray.add(icon1);
iconArray.add(icon2);
iconArray.add(icon3);
for(int i = 0; i < iconArray.size(); i++)
{
ImageView button= iconArray.get(i);
if(Global_Class.getInstance().getValue().tags.size() == 1)
{
if(Global_Class.getInstance().getValue().tags.get(i) == null)//Here is where Its giving me an ERROR!!!!!
{
button.setImageResource(R.drawable.icon_blank);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
}
else if(Global_Class.getInstance().getValue().tags.size() == 2)
{
if(Global_Class.getInstance().getValue().tags.get(i) == null)
{
button.setImageResource(R.drawable.icon_blank);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
else
{
//
}
}
else
{
if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
else
{
//
}
}
}
}
Here is where i make tags arraylist:
public class GlobalVariables
{
public ArrayList<String> tags = new ArrayList<>();
}
And I add to it in another class:
public void onClick(View v)
{
switch (v.getId())
{
case R.id.imageButton:
if(food_pressed)
{
ImageButton food_button = (ImageButton) findViewById(R.id.imageButton);
food_button.setImageResource(R.drawable.pressed_food);
tags.add("food");
food_pressed = false;
break;
}
else
{
ImageButton food_button = (ImageButton) findViewById(R.id.imageButton);
food_button.setImageResource(R.drawable.icon_food);
tags.remove("food");
food_pressed = true;
break;
}
case R.id.imageButton9:
ImageButton done_button = (ImageButton) findViewById(R.id.imageButton9);
done_button.setImageResource(R.drawable.pressed_done);
Global_Class.getInstance().getValue().tags = tags;
//Toast.makeText(getApplicationContext(),Global_Class.getInstance().getValue().tags.toString(),Toast.LENGTH_SHORT).show();
startActivity(toDescription);
break;
etc...
}
first time asking around here. I'm doing an AVLTree generic and there is a method in the Node that gives me an Iterator that returns me a Object array. The thing is that when I try to get that array with the objects of one of my none generic classes it sends me this.
Exception in thread "main" java.lang.ClassCastException: [LestructuraDeDatos.NodoAVL; cannt be cast to [Lmundo.Categoria;
Here is the node class
public class NodoAVL <T extends Comparable <T>>
{
//--------------------------------------------------------------------------------------------
//Atributos
//--------------------------------------------------------------------------------------------
private NodoAVL<T> izquierdo;
private NodoAVL<T> derecho;
private T elemento;
private String criterio;
//--------------------------------------------------------------------------------------------
//Constructor
//--------------------------------------------------------------------------------------------
public NodoAVL(T elem, String crit)
{
elemento = elem;
izquierdo = null;
derecho = null;
criterio = crit;
}
//--------------------------------------------------------------------------------------------
//Metodos
//--------------------------------------------------------------------------------------------
public NodoAVL<T> getIzquierdo() {
return izquierdo;
}
public void setIzquierdo(NodoAVL<T> izquierdo) {
this.izquierdo = izquierdo;
}
public NodoAVL<T> getDerecho() {
return derecho;
}
public void setDerecho(NodoAVL<T> derecho) {
this.derecho = derecho;
}
public String getCriterio() {
return criterio;
}
public void setCriterio(String criterio) {
this.criterio = criterio;
}
public boolean soyHoja()
{
if(izquierdo == null && derecho == null)
{
return true;
}
return false;
}
public T getElemento() {
return elemento;
}
public void setElemento(T elemento) {
this.elemento = elemento;
}
public void agregarElemento(T elemento, String Crit)
{
//Buscarlo antes de agregar, no puede haber iguales en el arbol
if(buscarElemento(Crit)==null)
if(soyHoja())
{
if(elemento.compareTo(this.elemento)>0)
{
NodoAVL<T> nuevo = new NodoAVL<T>(elemento, Crit);
setDerecho(nuevo);
}else if(elemento.compareTo(this.elemento)<0)
{
NodoAVL<T> nuevo = new NodoAVL<T>(elemento, Crit);
setIzquierdo(nuevo);
}
}else
{
NodoAVL<T> nuevo = new NodoAVL<T>(elemento,Crit);
if(this.elemento.compareTo(elemento)>0 && izquierdo == null)
{
izquierdo = nuevo;
}
else if(this.elemento.compareTo(elemento)>0)
{
izquierdo.agregarElemento(elemento,Crit);
}
else if(this.elemento.compareTo(elemento)<0 && derecho == null)
{
derecho = nuevo;
}
else if( this.elemento.compareTo(elemento)<0)
{
derecho.agregarElemento(elemento, Crit);
}
}
balanciarSubArbol();
}
public NodoAVL<T> rotarIzq(NodoAVL<T> rotar)
{
NodoAVL<T> temp = rotar.derecho;
rotar.setDerecho(temp.izquierdo);
temp.setIzquierdo(rotar);
return temp;
}
public NodoAVL<T> rotarDer(NodoAVL<T> rotar)
{
NodoAVL<T> temp = rotar.izquierdo;
rotar.setIzquierdo(temp.derecho);
temp.setDerecho(rotar);
return temp;
}
public int darBalance()
{
if(soyHoja())
{
return 0;
}
else
{
int izq = (izquierdo == null)?0:izquierdo.darAltura();
int der = (derecho == null)? 0 :derecho.darAltura();
return (izq - der);
}
}
public NodoAVL<T> dobleRotacionDerIzq(NodoAVL<T> nodo)
{
nodo.setDerecho(rotarDer(nodo.getDerecho()));
return rotarIzq(nodo);
}
public NodoAVL<T> dobleRotacionIzqDer(NodoAVL<T> nodo)
{
nodo.setIzquierdo(rotarIzq(nodo.getIzquierdo()));
return rotarDer(nodo);
}
public void balanciarSubArbol()
{
int valor = darBalance();
if(-2==valor || valor==2)
{
if(valor<0 && derecho.darBalance()<0)
{
if(derecho.darBalance()<-2)
{
derecho.balanciarSubArbol();
}else
{
rotarIzq(this);
}
}else if(valor<0 && derecho.darBalance()>0)
{
if(derecho.darBalance()>2)
{
derecho.balanciarSubArbol();
}else
{
dobleRotacionDerIzq(this);
}
}else if(valor>0 && izquierdo.darBalance()>0)
{
if(izquierdo.darBalance()>2)
{
izquierdo.balanciarSubArbol();
}else
{
rotarDer(this);
}
}else if(valor>0 && izquierdo.darBalance()<0)
{
if(izquierdo.darBalance()<-2)
{
izquierdo.balanciarSubArbol();
}else
{
dobleRotacionIzqDer(this);
}
}
}
}
public NodoAVL<T> eliminarElemento(T elemento)
{
if(soyHoja() && this.elemento==elemento)
{
return null;
}else if(soyHoja() && this.elemento!=elemento)
{
return this;
}
else
{
if(this.elemento.compareTo(elemento)==0)
{
if(izquierdo != null && derecho != null)
{
NodoAVL<T> temp = derecho;
izquierdo.setDerecho(temp.getIzquierdo());
temp.setIzquierdo(izquierdo);
return temp;
}
else if(izquierdo != null)
{
return izquierdo;
}
else
{
return derecho;
}
}
else if(this.elemento.compareTo(elemento)>0)
{
izquierdo = izquierdo.eliminarElemento(elemento);
return this;
}
else if(this.elemento.compareTo(elemento)<0)
{
derecho = derecho.eliminarElemento(elemento);
return this;
}
balanciarSubArbol();
return this;
}
}
public T buscarElemento(String criterio)
{
if(this.criterio.equalsIgnoreCase(criterio))
{
return this.elemento;
}
else
{
T izq = (izquierdo != null)?izquierdo.buscarElemento(criterio):null;
T der = (derecho != null) ? derecho.buscarElemento(criterio):null;
if(izq != null)
{
return izq;
}else if(der != null)
{
return der;
}
}
return null;
}
public IteradorAVL<T> darElementos()
{
IteradorAVL<T> ite = new IteradorAVL<T> (this);
return ite;
}
public int darPeso()
{
if(soyHoja())
{
return 1;
}else
{
int izq = (izquierdo == null)? 0: izquierdo.darPeso();
int der = (derecho == null) ? 0:derecho.darPeso();
return (izq+der+1);
}
}
public int darAltura()
{
if(soyHoja())
{
return 1;
}
else
{
int izq = ( izquierdo == null ) ? 0 : izquierdo.darAltura( );
int der = ( derecho == null ) ? 0 : derecho.darAltura( );
return(izq>der || izq == der)?izq+1:der+1;
}
}
}
and the iterator class
public class IteradorAVL<T extends Comparable <T>> implements Iterator<T>{
//--------------------------------------------------------------------------------------------
//Atributos
//--------------------------------------------------------------------------------------------
private NodoAVL<T> arbolitoAVL;
private Object [] elementos;
private int posActual;
private int total;
private Stack<NodoAVL> nodePath = new Stack<NodoAVL>();
//--------------------------------------------------------------------------------------------
//Constructor
//--------------------------------------------------------------------------------------------
public IteradorAVL ( NodoAVL <T> nodo)
{
arbolitoAVL = nodo;
posActual = 0;
total = nodo.darPeso();
elementos = new NodoAVL[total];
}
//--------------------------------------------------------------------------------------------
//Metodos
//--------------------------------------------------------------------------------------------
#Override
public boolean hasNext()
{
return(total>posActual)?true:false;
}
#Override
public T next() {
T siguienteT =null;
NodoAVL<T> respuesta = arbolitoAVL;
//Guardo el nodo actual en la lista
//Avancce
while (arbolitoAVL != null) {
nodePath.push(arbolitoAVL);
elementos[posActual] = arbolitoAVL;
arbolitoAVL = arbolitoAVL.getIzquierdo();
posActual++;
}
if (!nodePath.isEmpty()) {
arbolitoAVL = nodePath.pop();
posActual++;
elementos[posActual] = arbolitoAVL;
siguienteT = arbolitoAVL.getElemento();
arbolitoAVL = arbolitoAVL.getDerecho();
}
return siguienteT;
}
#Override
public void remove() {
// TODO Auto-generated method stub
}
public Object[] darArreglo()
{
return elementos;
}
The reason ClassCastException occurs is only one that your trying to typecast an object of one class to an object of another class which are not compatible.
Example :
Object i = Integer.valueOf(42);
String s = (String)i; // ClassCastException thrown here.