1

I feel this is a simple question, but I cannot find the write format online. How would I go about making a Comparable array of strings?

Like this:

Comparable<String> = {"A", "C", "J", "O", "Z"};

Or do I start it like:

Comparable<String> = new Comparable<String>(); 

Similar to an arraylist, I am not to sure.

I am using the Comparable interface so I can declare two lists, one of strings one of ints, and I may sort them using a specific sorting algorithm, all my current problem is how to initialize the array

Thanks

7
  • What do you want to do exactly? Do you want to compare String elements together? Commented Sep 13, 2015 at 21:32
  • How would the comparison work exactly? Commented Sep 13, 2015 at 21:33
  • Yes I am trying to compare strings in a sorting algorthm by input[j].compareTo(input[i]) < 0 And none of them compile Commented Sep 13, 2015 at 21:33
  • 1
    String itself will compare in a specific order, why do you still need to manually write a comparable for them? Commented Sep 13, 2015 at 21:34
  • What are you expect the comparison to do? I think there is no default implementation of this because there is no straight forward, always valid definition of the result of an array comparison. If your goal is to order the contents of the list (for example by sorting), than not the list has to be comparable but its elements. Commented Sep 13, 2015 at 21:34

3 Answers 3

1

You do not need to create an instance of Comparable. Because the String class already implements this interface, you can simply call the method .compareTo(String otherString)

ArrayList<String> myList = new ArrayList<String>();
myList.add("hello");
myList.add("hello");
System.out.println(myList.get(0).compareTo(myList.get(1))); //prints out 0

You can just populate the list like usual

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

4 Comments

An actual example of how to sort for the OP ? BTW, why do you use new to create your strings ? This prevents the JVM from interning the strings and does not really bring any value
@Dici Just out of habit; I'm used to using lists for other objects where you have to create new instantiations. Noted
Okay ! Just to make sure, do you know about interning ?
@Dici Yes; it's virtually always better to use 'myString" rather than new String("myString"). Again, it was just out of habit
1

Comparable<T> is an interface, so you can't directly declare anything to be of that type. The normal idiom for Comparable is that if you're declaring your own class to be comparable, you declare it like this:

class MyClass implements Comparable<MyClass> {
    // ... other fields, methods

    @Override
    public int compareTo(MyClass other) {
        ... something that returns <0, 0 or >0
    }
}

If you look at the language's definition of String here, you'll see that String already implements Comparable<String>. So a String is already comparable and already has a compareTo method. You don't need to do anything else to make it comparable.

To set up an array, the syntax is

String[] myArray = {"A", "C", "J", "O", "Z"};

To set up an ArrayList (which can grow or shrink),

ArrayList<String> myList = new ArrayList<>(Arrays.asList("A", "C", "J", "O", "Z"));

Comments

1

The OP's intent is not clear, but if the purpose of the question is how to create an array that is Comparable - since it's an interface that array does not implement, and since array cannot be overridden - it's simply not possible.

However, one can use a Comparator for a specific type for the purpose of comparing two instances of that type.

Most of the classes in Google Guava's com.google.common.primitives package have a static lexicographicalComparator() method returning a Comparator for arrays of the primitive type the class handles.

Guava currently does not have a similar solution for Object arrays, but if you can use an Iterable instead the static Comparators.lexicographical(Comparator) or the Ordering.lexicographical() on an existing Ordering might help.

Following what I understood as the OP's intent, let's assume we have two arrays:

String[] arr1 = {"A", "C", "J", "O", "Z"};
String[] arr2 = {"A", "C", "J", "O", "X"}; // different last element

We can now compare these two arrays using Ordering.lexicographical():

        int comparison = Ordering.natural()
                .lexicographical().compare(Arrays.asList(arr1), Arrays.asList(arr2));

The result will be positive, since arr1 is considered greater than arr2, because the first element in arr1 that was not equal to its corresponding element in arr2 was greater according to Ordering.natural().

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.