-2

I want to compare two numbers in arraylist,but I failed,anyone can help me,here is my code.

MainActivity{
ArrayList<User> users=new ArrayList<User>();
            users.clear();
            int imgid;
            int number=0;
            User use1;
            List<User> user=DataSupport.findAll(User.class);
            for (User use:user){
                use1=new User();
                use1.setNeednumber(use.getNeednumber());
                use1.setMaterialid(use.getMaterialid());
                users.add(use1);
            }
for(int i=1;i<users.size();i++){
                if(users.get(i-1).getMaterialid()==users.get(i).getMaterialid()){

number=number+users.get(i-1).getNeednumber();
                    Log.d("number",""+number);
                }
            }

On the whole,it's like this,I use Litepal to create an database,the database have a table called "User",the table have two column one is "Materialid" one is "Neednumber",I want read the data what I storage in this table and if the "Materialid" is equal let them add.I use above method but it didn't work,the number always zero.who can help me deal with this problem ,thank you very much.

L.M.Orz
  • 19
  • 4
  • 5
    [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/5221149) – Andreas Oct 06 '17 at 04:27
  • 2
    *"but I failed"* and *"it didn't work"* are not helpful descriptions. Also, did you try debugging to see what values you actually get for `materialid`? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) --- Even if there are multiple users with same material id, you are only comparing users that occur next to each other in the list. Is that really what you intended? – Andreas Oct 06 '17 at 04:30
  • refer https://stackoverflow.com/questions/19155283/simple-way-to-compare-2-arraylists – sasikumar Oct 06 '17 at 04:38
  • @Andreas that downvote description website is interesting, but something smells about storing topics on SO etiquette to someones personal repo. Guidelines are meant to be created and upheld by the SO community; not a single user. Maybe these posts could live in the meta pages somewhere? – flakes Oct 06 '17 at 04:47

1 Answers1

0

try this logic

    Integer numberArray[] = { 1, 2, 45, 6, 7, 7, 8, 9, 10, 11, 10, 9, 1 };
    Set<Integer> printed = new HashSet<>();
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (Integer s : Arrays.asList(numberArray)) {
        if (printed.add(s)) // Set.add() also tells if the element was in
                            // the Set!
            System.out
                    .println("element: " + s + ", count: " + Collections.frequency(Arrays.asList(numberArray), s));

        if (Collections.frequency(Arrays.asList(numberArray), s) > 1) {
            map.put(s, Collections.frequency(Arrays.asList(numberArray), s));
        }
    }
    Integer sum = 0;
    for (Map.Entry m : map.entrySet()) {
        System.out.println(m.getKey() + " " + m.getValue());
        sum = sum + ((Integer) m.getKey() * (Integer) m.getValue());
    }
    System.out.println("sum is" + sum);