Home java Comparison of Java array values ​​

Comparison of Java array values ​​

Author

Date

Category

There is an array, in it n numbers (both fractional and integer). There is a task to compare the values ​​of the array, and when the coincidence is to display \ write to the file.

Specifically, it is used in a script that provides the display of current balances on the site when loading information, two arrays are compared (the one that is and new), and the values ​​that coincided are output. For example, x = 1 and y = 1 .

How to make it so that if these values ​​are equal, then the action occurred (entry into a file, output, etc.)? How can I check the values ​​for the coincidence?


Answer 1

If I understand the essence of the task, then here:

for (int i = 0; i & lt; list.length; i ++) {
  for (int i2 = 0; i2 & lt; list.length; i2 ++) {
    if (i2 == i) Continue;
    If (List [i2] == List [i]) {
      / * If the coincided * /
      Break;
    }
  }
}

This option displays the match only once

arraylist & lt; float & gt; Found = New ArrayList & LT; Float & gt; ();
For (int i = 0; i & lt; list.length; I ++) {
  for (int i2 = 0; i2 & lt; list.length; i2 ++) {
    if (i2 == i) Continue;
    If (List [i2] == List [i]) {
      if (found.contains (LIST [I]) Continue;
      Found.add (List [i]);
      / * If the coincided * /
    }
  }
}

Answer 2

You can use such a cycle …

for (i = 0; i & lt; list.length; i ++) {
  If (List [i] = List [i]) {
    System.Out.printLN (List [i]);
  }
  ELSE {
    List [i] ++
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions