Results 1 to 4 of 4
- 03-09-2008, 12:08 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
Vectors of Vectors or hash-somethings?
Hi All,
I have an object called PICSet, which stores info about groups of pictures.
Each PICSet instance has a Vector of Associated Images (strings)
Each PICSet instance also has a Vector of Features (AU's) visible in that group..
I have another class which manipulates a vector of these PICSet objects (so there are Lots of sessions/groups)
Given a particular AU feature, I want to find All associated images from the Vector PICSets.Java Code:class PICSet{ public String SUBJECT; public String SESSION; public Vector ASSOCIATEDIMAGES; // actually contains Strings (filenames) public Vector AUsPresent; // actually contains ints public PICSet(String subj, String sess) public String toString() public void AssociateAU(int AU) } class PICSetManager{ Vector PICSets = new Vector(); }
What's the easiest - most efficient - way of doing this?
If there's a better way of doing this (including HashTable/Maps etc) I'd like to know please...
__________
MindWarpLast edited by mindwarp; 03-09-2008 at 01:28 PM. Reason: technical error in code description
- 03-09-2008, 09:39 PM #2
Java Code:import java.util.*; import java.util.List; public class PICTest { // Make up some AU constants. static final int A = 1; static final int B = 2; static final int C = 3; static final int D = 4; public static void main(String[] args) { PICStoreManager manager = new PICStoreManager(); // Add some PICStores. addStores(manager); // Add some image fileNames. addImageNames(manager); // Add some AU features. addFeatures(manager); for(PICStore store : manager.picStores) System.out.println(store); // Collect the image filePaths for the AU feature A. List<String> list = manager.getImagesByAU(A); for(int j = 0; j < list.size(); j++) { System.out.println((String)list.get(j)); } } private static void addStores(PICStoreManager manager) { // Make up some PICStores to manage. String[] subjects = { "town hall", "steeple", "clown", "river" }; String[] sessions = { "first", "ninth", "third", "sixth" }; List<PICStore> picStores = manager.picStores; for(int j = 0; j < subjects.length; j++) { picStores.add(new PICStore(subjects[j], sessions[j])); } } private static void addImageNames(PICStoreManager manager) { String[][] fileNames = { { "entrance.jpg" }, { "center.png", "top.jpg", "detail.jpg" }, { "face.gif", "hands.jpg" }, { "surface.gif", "southBend.png", "bottom.gif", "swirl.jpg" } }; List<PICStore> picStores = manager.picStores; for(int j = 0; j < picStores.size(); j++) { PICStore store = (PICStore)picStores.get(j); for(int k = 0; k < fileNames[j].length; k++) { store.addImageName(fileNames[j][k]); } } } private static void addFeatures(PICStoreManager manager) { int[][] features = { { C, A }, { B, D, C }, { A }, { B, A } }; List<PICStore> picStores = manager.picStores; for(int j = 0; j < picStores.size(); j++) { PICStore store = (PICStore)picStores.get(j); for(int k = 0; k < features[j].length; k++) { store.AssociateAU(features[j][k]); } } } } class PICStoreManager { List<PICStore> picStores = new ArrayList<PICStore>(); public void addPICStore(PICStore store) { picStores.add(store); } public List<String> getImagesByAU(int AU) { Integer target = Integer.valueOf(AU); List<String> list = new ArrayList<String>(); for(PICStore store : picStores) { if(store.AUsPresent.contains(target)) { // Found the target AU feature in this store. // Add all the image pathStrings to list. list.addAll(store.ASSOCIATEDIMAGES); } } return list; } } class PICStore { public String SUBJECT; public String SESSION; public List<String> ASSOCIATEDIMAGES; // contains Strings (filenames) public List<Integer> AUsPresent; // contains ints public PICStore(String subj, String sess) { SUBJECT = subj; SESSION = sess; ASSOCIATEDIMAGES = new ArrayList<String>(); AUsPresent = new ArrayList<Integer>(); } public String toString() { return getClass().getName() + "[SUBJECT:" + SUBJECT + " SESSION:" + SESSION + "]"; } public void addImageName(String name) { ASSOCIATEDIMAGES.add(name); } public void AssociateAU(int AU) { // What if AUsPresent already contains this AU feature? AUsPresent.add(new Integer(AU)); } }
- 03-10-2008, 09:06 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
better solution?
:confused: Hi, thanks for answering
I have some questions on your code:
In PICStoreManager, why did you use " Integer target = Integer.valueOf(AU); "
instead of " int target = Integer.valueOf(AU); " ????? Why are you wrapping the int into an Integer?
In PICStore.AssociateAU(int AU) wrt the comment - I first check that it's not already there..
Also, I did a small experiment:
Java Code:import java.util.*; public class vectortest{ static class newobj{ int val; int val2; public newobj(int v1,int v2){ val = v1; val2 = v2;} public String toString(){ return ( "[" + getClass().getName() + ": " + val + ", " + val2 + "]" ); } } public static void main(String[] args){ Vector V1 = new Vector(); for (int i = 0; i< 6; i++) V1.add(i); for (int i = 0; i< 5; i++) V1.add((char)('a'+ i)); for (int i = 27; i< 31; i++) V1.add(i); boolean b = true; for (int i = 0; i < 5; i++){ V1.add(b); b = (b ? false: true); } for (int i = 0; i < 5; i++) V1.add(new newobj(i,i+2)); for (int j = 0; j<V1.size(); j++){ System.out.print("{" + (""+V1.elementAt(j)).toString()+ "}"); } System.out.println(); System.out.println("vector contains a 3: " + V1.contains(3)); System.out.println("vector contains a 5: " + V1.contains(5)); System.out.println("vector contains a a: " + V1.contains('a')); System.out.println("vector contains a e: " + V1.contains('e')); System.out.println("vector contains a 30: " + V1.contains(30)); System.out.println("vector contains a 31: " + V1.contains(31)); newobj no = new newobj(1,3); System.out.println("vector contains a newobj(1,3):" + V1.contains(no)); System.out.println(); } }
output:{0}{1}{2}{3}{4}{5}{a}{b}{c}{d}{e}{27}{28}{29}{30}{ true}{false}{true}{false}{true}{[vectortest$newobj: 0, 2]}{[vectortest$newobj: 1, 3]}{[vectortest$newobj: 2, 4]}{[vectortest$newobj: 3, 5]}{[vectortest$newobj: 4, 6]}
vector contains a 3: true
vector contains a 5: true
vector contains a a: true
vector contains a e: true
vector contains a 30: true
vector contains a 31: false
vector contains a newobj(1,3):false
So, why is it not easier to do the following (looks a lot like your code)?
Changing to Lists will mean significant rewriting of my code, but if the advantage is that much greater then it is an option I'll look at.Java Code:public String getImagesByAU(int AU){ // untested, pls excuse any typos Vector imgsFound = new Vector(); for (int i = 0; i< PICSets.size(); i++){ if (((PICSet) (PICSets.elementAt(i))).AUsPresent.contains(AU)) imgsFound.addAll( (PICSet) (PICSets.elementAt(i).ASSOCIATEDIMAGES) ); } return imgsFound; }
Would you like to deliberate?
Also, I was hoping for a solution using a structure that could automatically map them AU features to ASSOCIATEDIMAGES, if at all possible (without writing the structure myself)
- 03-10-2008, 02:57 PM #4
In PICStoreManager, why did you use " Integer target = Integer.valueOf(AU); "
instead of " int target = Integer.valueOf(AU); " ????? Why are you wrapping the int into an Integer?
Collections take Objects and not primitives. The auto–boxing feature in j2se 1.5+ allows us to overlook this.
In PICStore.AssociateAU(int AU) wrt the comment - I first check that it's not already there..
Just another option: whether you want multiple AU feature constants in the collection or not. Depends on what you are doing.
So, why is it not easier to do the following (looks a lot like your code)?
Looks like you have things figured out the way you want them.
Changing to Lists will mean significant rewriting of my code, but if the advantage is that much greater then it is an option I'll look at.
Vectors are fine.
Similar Threads
-
Help with Vectors and Strings...
By kaban in forum New To JavaReplies: 2Last Post: 12-09-2007, 09:04 AM -
Vectors vs ArrayList
By Java Tip in forum Java TipReplies: 0Last Post: 11-28-2007, 10:29 AM -
Understanding Vectors
By cbrown08 in forum New To JavaReplies: 7Last Post: 11-05-2007, 06:56 PM -
Problem with vectors in java
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks