0

I had asked the same question in another forum, but dint get any suitable answers...so Im posting it here. I have the following program:

    public void execute(){  

      public static ArrayList<Long> time = new ArrayList<Long>();  
      public static ArrayList<Integer> state = new ArrayList<Integer>();  
      public static ArrayList<Integer> cpu = new ArrayList<Integer>();  

     for(int i=0; i<time.size(); i++){  

        if(cpu.get(i).equals(get)){  

        Long next_time = time.get(i);  
        Integer next_func = state.get(i);  
        Integer next_proc = cpu.get(i);  

            if(next_time.equals(g) && (next_func.equals(test1.func_num))){  



                Integer func_next = stt.get(i+1);  

                if(func_next.equals(0)||(func_next.equals(next_func))) {  
                    System.out.println("here");  
                }  

                else   
                    System.out.println("here");  
                    if(cpu.get(i+2).equals(get))  
                        if(stt.get(i+2).equals(func_next) || (stt.get(i+2).equals(0)))    
                            System.out.println(stt.get(i+2));  

            }  
    }  

    }  

What I want to do is this: I get the value of time, cpu and state from the user. find the match in the arraylist for the corresponding values, then I want to loop through the arraylists for only those values which match the 'cpu'. All the ArrayLists are of same size and contain values corresponding to each other at any given index. How can I do this?

Example: The ArrayLists contain various values as follows:

time = 1 cpu = 12 state = 24
time = 2 cpu = 12 state = 4
time = 5 cpu = 13 state = 23
time = 6 cpu = 13 state = 26
time = 8 cpu = 11 state = 34
time = 11 cpu = 12 state = 54
time = 13 cpu = 12 state = 56
time = 14 cpu = 11 state = 58
time = 15 cpu = 15 state = 46

This is the situation. And I get value from the user as time=2 cpu=12 state =4....I find the match and after that I want to look for all values corresponding to cpu=12 only..

8
  • If the user provides all three values (time, state, and cpu), why do you still need to loop through the lists? What do you want to know? The number of occurences? The indices? Commented Jun 25, 2013 at 7:44
  • the model you describe is not what your code shows... what is 'user'? What do you mean with 'match'? And also: Is the code you show complete? if not, why, where? what does not work? Commented Jun 25, 2013 at 7:48
  • Given your assignment, I would recommend moving to some database. There is few in-memory databases written in java (H2 SQL, for example). Then it would be much easier. Commented Jun 25, 2013 at 7:48
  • @all please see my example Commented Jun 25, 2013 at 7:50
  • @Paolof76 please see my example which I have added in my post now Commented Jun 25, 2013 at 7:55

4 Answers 4

2

Base more on the description then code example

You get a input in form of time, cpu and state form user. You want to find match for those input criteria.

To be able to do that easily, You should create a type for that.

public class Data {

 private final int   cpu;
 private final long time;
 private final int state;  

 public Data(int cpu, long time, int state) {
    this.cpu   = cpu;
    this.time  = time;
    this.state = state;
 }

 //add implementation for equals and hashcode methods. 

}

The equals and hash code method are responsible to define unique value for object. So when you create an object with the same input the should generate same hashcode.

The you create your collection with those elements

Set<Data> storage = new HashSet<Data>();

in this storage, you should store all data that you want to execute search on.

The search is simple. You create a search item

Data searchItem = new Data(user.getCpu(), user.getTime(), user.getState());

if(storage.contains(searchItem)) {
  // action on true
} else {
 // action on false
}

Implementing hash code

EDIT:

Q: How to perform on all items for given CPU ?

To support such operation you must have in your code a structure that can deliver you some sort of data based on decision. Typically for this operation is used type Map. This type allow to gather under a key reference to value. The value can be a collection of objects.

Map> dataMap = new HashMap<>();// Java diamond notation.

or you can use [Multimap] from guava.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes this works good for the search...but after finding the correct match for time,cpu, state...I want to loop through the ArrayLists for values which match the given CPU only...how do I do that?
0

When you find the match, you do this:

//once you have the index in a Integer var called myVal
Set<Integer> indexes = new HashSet<Integer>();
for(int i=0; i<time.size(); i++){
   if (cpu.get(i) == myVal) {
      indexes.add(i);
   }
}

Now you can use the set of indexes:

for (Integer index: indexes) {
   //do whatever
}

This is O(time.size()). Hope this helps

Comments

0

Java is pure oo language. It means not only you must write in Object Oriented Style, but also think everything as objects like real world. Before finding how to solve this problem, I would like to advise you that you should read carefully OOP and Connections framework in Java.

1 Comment

Firstly Java is not a pure OO language (primitives are not objects, then there is null etc). Plus there are some new OO paradigms that it does not have (traits, mixins), though some of them will be incorporated in Java 8. But yes, I do get the context.
0

Something like this should work:

bool matchFound = false;
for (int i = 0; i < time.size(); i++) {
    long thisTime = time.get(i);  
    int thisState = state.get(i);  
    int thisCpu = cpu.get(i);

    if (matchFound) {
        if (thisCpu == userCpu) {
            System.out.println("Time: " + thisTime + " "
                             + "State: " + thisState + " "
                             + "Cpu: " + thisCpu);
        }
    } else {
        matchFound = (thisTime == userTime 
                   && thisState == userState
                   && thisCpu == userCpu);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.