Compare two ints lines in java

I have two lines containing numbers, and I want to see if the second line contains the same numbers as the first line, regardless of whether they are in order or not. If it has a number repeating than false. Is there anything other than using .charAt () in java because it doesn't work for numbers after 10?

String one = "1 2 3 4 5 "; 
String two = " 3 2 1 4 5 ";
String three = "3 2 1 4 4 "; 
+5
source share
8 answers

It seems like homework. So follow these steps:

  • Trim both lines
  • Convert both lines to ArrayList using space delimiter
  • Sort both arrays numerically
  • Compare both arrays
+7

Scanner.nextInt() , Set , set1.equals(set2).

+4

. String List<Integer> String.split() Integer.parseInt() . sort() , .

+2

.

String one = "1 2 3  4 5 ";
String two = " 3 2 1 4 5 ";
Set<String> a = new HashSet<String> (Arrays.asList(one.trim().replaceAll("\\s*"," ").split(" ")));
Set<String> b = new HashSet<String> (Arrays.asList(two.trim().replaceAll("\\s*"," ").split(" ")));
boolean ret = (a.size() == b.size()) && a.containsAll(b);
+2

/ , , .

0

( String.split, StringTokenizer), . HashSet. . , , - , HashSet .

0

, :

  import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;


public class StringsCompare {

    private static String one = "1 2 3 4 5 6"; 
    private static String two = " 3 2 1 5 6 4 ";

    public static void main(String[] args) {
        StringsCompare sc = new StringsCompare();

        System.out.println(sc.compare(one, two));
    }

    private boolean compare(String one, String two) {

        SortedSet<Integer> setOne = getSet(one);    
        SortedSet<Integer> setTwo = getSet(two);

        return setOne.equals(setTwo);
    }

    private SortedSet<Integer> getSet(String str) {
        SortedSet<Integer> result = new TreeSet<Integer>();

        StringTokenizer st = new StringTokenizer(str, " ");

        while (st.hasMoreTokens()) {
            result.add(Integer.valueOf(st.nextToken()));
        }

        return result;
    }
}
0

int.

Integer.parseInt(One).intValue() == Integer.parseInt(two).intValue()

, , , .

-1

All Articles