I was using Arrays.sort() function to sort 2d array (int[][] array). Since I want to sort it base on the first element. For example, {{2,3},{1,4}} base on 1st element the array will be {{1,4},{2,3}}. So I override the compare function.
Arrays.sort(arr, new Comparator<int[]>() {
#Override
public int compare(int[] o1, int[] o2){
if(o1[0] < o2[0]){
return -1;
} else if (o1[0] > o2[0]) {
return 1;
} else {
return 0;
}
}
})
I know this sort work. But I don't understand how this compare work.
I was thinking the
new Comparator<int[]>
should be
new Comparator<int[][]>
since this is 2d array. and inside of compare function should be compare
o1[0][0] and o2[0][0]
Can anyone help me understand it?
Also this is using Arrays.sort, can I use Collections.sort? what is different between it?
Thanks
Remember that a "2D array" doesn't actually exist in Java, so what you're really dealing with is "an array of int[]" (there's nothing inherently preventing each of those int[] from being a different length).
So: when you sort, you're comparing individual elements of that "array of int[]" with each other, and because each element is an int[], your Comparator is for int[], too.
You are passing the array in the sort method and giving it a comparator. Arrays.sort will use iterator to pass into compare method. So compare method is checking element at arr[0].compare(arr[1]) sorts these 2 and goes to next iterator. same concept applies using 2d array. You are just passing 2 arrays and telling compare who should be placed where.
Related
I am working on 2D array and I need to organize my array.
Explanation, my array is an Array String and it's containing in the header some double value, and I want to sort the array in function of the header.
The first thing I thought was to get the header and sort the header, get all the columns of the array in another array and comparate the value of the header ordered to each column index[0] and push to another array.
However, I thought there is a way to do it easily, but I don't know if there is, I saw the possibility to sort directly in the array and organize in function of this but I have no idea how to it.
Just in case :
Original array
String[][]myArray = {{3,Toto,30},{2,Tata,29},{1,Titi,13}};
Array expected
String[][]newArray = {{1,Titi,13},{2,Tata,29},{3,Toto,30}};
Open to proposal!
Thanks.
Edit : The header could be Double value.
If I understood you correctly, seems like this:
Arrays.stream(myArray)
.sorted(Comparator.comparingDouble(x -> Double.valueOf(x[0])))
.toArray(String[][]::new);
Of course you can do that in place too, via Arrays::sort and that Comparator
Previous answer just returned the original array when I tried it. Here's what worked for me:
String[][] myArray = {{"3","Toto","30"},{"2","Tata","29"},{"1","Titi","13"}};
Arrays.sort(myArray, new Comparator<String[]>() {
public int compare(String[] lhs, String[] rhs) {
try {
double lhs_value = Double.parseDouble(lhs[0]);
double rhs_value = Double.parseDouble(rhs[0]);
if (lhs_value < rhs_value) return -1;
if (lhs_value > rhs_value) return 1;
return 0; //else the two are equal
} catch (NumberFormatException e) {
//handle exception
return 0;
}
}
});
My output:
1 Titi 13
2 Tata 29
3 Toto 30
Here Arrays.sort (see Javadoc) takes in two parameters: an array you're sorting (myArray), and a Comparator (see Javadoc), which is an interface that allows comparison between two arbitrary types. Since {"3", "Toto", "30"} isn't a type you created, and is just a String[], we're going to make a Comparator<String[]> inline.
Comparators implement a function "compare" which takes in two elements, and returns -1, 0, or 1 to determine the ordering of the elements. Essentially "compare" gets called multiple times in the sorting process to precisely determine sorted order. Here's some pseudocode:
public int compare(Object a, Object b)
if (a comes "before" b) return -1
if (a is "equal" to b) return 0
if (a comes "after" b) return 1
If that isn't clear, you can learn more about the Comparator interface here:
https://www.geeksforgeeks.org/comparator-interface-java/
I know I used "before" and "after" earlier, which are a little fuzzy and non-rigorous. If you're more mathematically inclined, here's a good discussion of the Comparator interface:
https://math.stackexchange.com/questions/1400655/java-comparator-documentation-confused-about-the-terminology-total-order
I started using the Android SparseArray recently in place of in place of ArrayList for small numbers of items. I am wondering why it does not contain a toArray() method like a normal ArrayList? It contains a private objects array for values but no way of returning them. It seems to me that this would be a useful method to have as ArrayList to array conversions are common occurrences in Android. I am assuming that because of the way hashing works it would return an array with empty slots?
My second question is, what would be the most efficient way to convert a SparseArray to a normal Object array?
To answer the title of your question, there is no method for this because the SparseArray is not an array, it is in fact an optimization of a HashMap, binding arbitrary integers to arbitrary values. The optimization lies in the fact that it uses no boxing/unboxing where a HashMap would.
Then the second part, how you should convert it to an array, I would recommend determining the largest array index using size() and keyAt(index), and then allocating an array of the appropriate size, and finally copying all values using the get(index, default) method with a sensible default.
theres no such method to do that, but you can convert the SparseArray iterating :
public static <C> List<C> ConvertToList(SparseArray<C> sparseArray) {
if (sparseArray == null) return null;
List<C> arrayList = new ArrayList<C>(sparseArray.size());
for (int i = 0; i < sparseArray.size(); i++)
arrayList.add(sparseArray.valueAt(i));
return arrayList;
}
Kotlin version:
private inline fun <reified VALUE> SparseArray<VALUE>.toArrayOrNull(): Array<VALUE>? {
return if (isNotEmpty()) {
Array<VALUE>(size()) { index -> this[index] }
} else {
null
}
}
Is there a way i could return in an array the common elements of 2 or more arrays? I know having some of the methods under lists could do it but is there a way to do it by only using arrays? I made my own get and length btw since i am creating a an array called OrderedIntList.
Example would be:
1,3,5
1,6,7,9,3
1,3,10,11
Result: 1,3
I tried this and it outputs the common elements between two arrays and not all.
I know there's something wrong but i do not how to make it work like it suppose to work :(
//returns the common elements of inputted arrays
public static OrderedIntList common(OrderedIntList ... lists){
int[] list = new int[10];
for(int x = 1; x <= lists.length -1; x++){
for(int q = 0; q < lists[0].length()-1; q++) {
for(int z = 0; z < lists[x].length(); z++) {
if (lists[0].get(q)==lists[x].get(z)){
list[q] = lists[0].get(q);
}
}
}
}
OrderedIntList newlist = new OrderedIntList(list);
return newlist;
}
This can be an easy algorithm to solve it...
1) Instantiate an instance variable of type array called
"commonElements" pointing to the elements of the first Array. At the
beginning these are your common elements.
2) Create a method call getCommonElements(int[] commonElements,
int[] newList). This method manipulates the commonElements array to leave
it with only the common elements between the two. (p.s Use a temporary
array to achieve this if you find it easier)
3) Iterate over all the arrays present in "lists" starting from the
second array.
4) call the method at point 2 for each array .
All the difficult part for you it's to implement a method that given 2 arrays finds the common elements!
You can use
org.apache.commons.collections.CollectionUtils.intersection(java.util.Collection a, java.util.Collection b)
to get the intersection of two lists (elements presents in both lists)
And to pass your array as a Collection: java.util.Arrays.asList(Object[] a);
But working on arrays is tedious, at best. You should consider why you don't want to use a Collection...
As a partial answer, you're probably doing too much work by fully reimplementing an OrderedIntList the way you're doing, since ArrayList and friends already come with sorting baked in via the Collections class.
import java.util.Collections;
public class OrderedIntList extends ArrayList<Integer> {
#override // to effect sorted inserts
public void add(Integer i) {
this.add(i);
Collections.sort(this);
// done.
}
}
Wanting to do this for pure arrays is a nice exercise, but then you'll be better of implementing sorting properly with a quick sort (you can't websearch for a java implementation of that without getting a million results) or an insert sort (equally websearchable), and follow the same recipe.
any time you push a number into the array:
guess where the number goes (although that's optional),
insert the number,
resort your array if you know your guess wasn't perfect.
An assignment we have to do in school is alphabetically sort two different ArrayLists into a final arraylist that contains both of the lists elements lexicographically
For example...
ArrayList sent1 contains in alphabetical order [., adam, mark]
ArrayList sent2 contains in alphabetical order [., betsy, kyle, william]
ArrayList sent3 must contain all of the elements in both ArrayLists in alphabetical order.
I just can't seem to figure out how to do it. My instructor mentioned using a while loop, but I don't just understand how to code the process. Combining sent1 and sent2 into sent3 and then using Collections.sort(sent3) would make the process so much easier and more compact, but the goal of the lab is to practice the using of compareTo().
If each source list is already sorted, just look at the first element in each list, add the "lower" (alphabetically first) to the output list, and repeat.
It's pretty simple;
List<String> sent3 = new ArrayList<String>(sent1);
sent3.addAll(sent2);
Collections.sort(sent3, new Comparator<String>() {
public int compare(String a, String b) {
return a.compareTo(b); // usage here!
}
}
What you are looking for is to use 2 iterators and just merge these 2 lists.
I'll give you some pseudo code.
ArrayList sent1, sent2, sent3;
int i1, i2;
i1 = i2 = 0;
while (i1 < sent1.length() && i2 < sent2.length())
{
if (sent1[i2].compareTo(sent2[i1])
{
sent3.add(sent1[i2]);
i2++;
}else
{
sent3.add(sent1[i1]);
i21+;
}
}
while(i1 < sent1.length()) sent3.add(sent1[i1++]);
while(i2 < sent2.length()) sent3.add(sent2[i2++]);
Here each time will add to my sent3 the minimum of the first items in sent1 and sent2. And when I get one of this lists empty, I will just append the rest of other elements into the sent3.
I think that you can do this:
copy array1 and array2 to array3; then, sort array3. you don't know how to sort using compareTo() ? Check is java documentation.
This is probably a simple question, but I have two arrays of approx 1000 elements each, they are called posXArray and posYArray. I want to sort posYArray numerically (lowest number first) but I want the elements of posXArray to have the same operation applied to them...
For example, if element [56] of posYArray is the smallest one , I want element [56] of posXArray to also be moved to [0].
How is this implemented in Java in an easy/good way?
Thank you very much for you help!
Since the arrays seem to contain X and Y coordinates, perhaps a better choice is to create a coordinate class containing both values, implement Comparable and just have a single array to sort using the built-in algorithms?
Create a class like:
public class XYPos implements Comparable<XYPos> {
int x;
int y;
#Override
public int compareTo(XYPos o) {
int res = this.y - o.y;
if(res == 0) {
res = this.x - o.x;
}
return res;
}
}
Then:
convert your 2 arrays into one array of XYPos
sort it
update your 2 original arrays with the values in the sorted array
Create an Integer[] idx of the same length and fill it with the numbers 0 to 999 (or whatever), then sort this array using a comparator that does
public int compare(Integer a, Integer b) {
return posYArray[a] - posYArray[b];
}
This will give you an array of indexes into the other arrays, i.e. the smallest Y value will be posYArray[idx[0]] and its corresponding X will be posXArray[idx[0]], etc. If you don't want to maintain the indirection you could re-order the original arrays by the idx values.
If you're doing this sort of thing regularly you may wish to look at fastutil which provides Collection and Comparator types that operate directly on primitive types such as int, avoiding the need to box and unbox Integers.
You could implement any sorting algorithm of your choice, do the comparation check on the first (posXArray) only, and then swap position of elements in both arrays at the same time.