When to use Comparable[ ] in Java?Is Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Does a finally block always get executed in Java?What is the difference between public, protected, package-private and private in Java?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Comparing Java enum members: == or equals()?How do I convert a String to an int in Java?Creating a memory leak with Java
Are there any crystals that are theoretically possible, but haven't yet been made?
Why would Thor need to strike a building with lightning to attack enemies?
What were the "pills" that were added to solid waste in Apollo 7?
Create a min stack
Does the Aboleth have expertise in history and perception?
What does it mean for a program to be 32 or 64 bit?
Germany rejected my entry to Schengen countries
Have I found a major security issue with login
Service 'iisadmin' (iisadmin) failed to start
Is there any official Lore on Keraptis the Wizard, apart from what is in White Plume Mountain?
Why does the U.S military use mercenaries?
Chain rule instead of product rule
How come Arya Stark wasn't hurt by this in Game of Thrones Season 8 Episode 5?
What's is the easiest way to purchase a stock and hold it
Would it be possible to set up a franchise in the ancient world?
Isn't Kirchhoff's junction law a violation of conservation of charge?
Is being an extrovert a necessary condition to be a manager?
Why aren't satellites disintegrated even though they orbit earth within earth's Roche Limits?
Vehemently against code formatting
Why are Marine Le Pen's possible connections with Steve Bannon something worth investigating?
How to plot a surface from a system of equations?
If a foot soldier had to kill a dragon, how would he go about it?
How to fix "webpack Dev Server Invalid Options" in Vuejs
How does the "reverse syntax" in Middle English work?
When to use Comparable[ ] in Java?
Is Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Does a finally block always get executed in Java?What is the difference between public, protected, package-private and private in Java?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Comparing Java enum members: == or equals()?How do I convert a String to an int in Java?Creating a memory leak with Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I was watching over a code which was using Comparable[] as an array that is exactly working as a collection framework list.As the task was done easily by using it without too many loops, is it a good practice to use it for other data types?
public class MergeSortExample
public Comparable[] mergeSort(Comparable[] inputList)
if(inputList.length <= 1)
return inputList;
Comparable[] list1 = new Comparable[inputList.length/2];
Comparable[] list2 = new Comparable[inputList.length - list1.length];
System.arraycopy(inputList, 0, list1, 0, list1.length);
System.arraycopy(inputList, list1.length, list2, 0, list2.length);
mergeSort(list1);
mergeSort(list2);
merge(list1, list2, inputList);
return inputList;
public void merge(Comparable[] list1, Comparable[] list2, Comparable[] resultList)
int indexOfList1 = 0;
int indexOfList2 = 0;
int indexOfMergedList = 0;
while(indexOfList1 < list1.length && indexOfList2 < list2.length)
if(list1[indexOfList1].compareTo(list2[indexOfList2]) < 0)
resultList[indexOfMergedList] = list1[indexOfList1];
indexOfList1++;
else
resultList[indexOfMergedList] = list2[indexOfList2];
indexOfList2++;
indexOfMergedList++;
System.arraycopy(list1, indexOfList1, resultList, indexOfMergedList, list1.length - indexOfList1);
System.arraycopy(list2, indexOfList2, resultList, indexOfMergedList, list2.length - indexOfList2);
java comparable
add a comment |
I was watching over a code which was using Comparable[] as an array that is exactly working as a collection framework list.As the task was done easily by using it without too many loops, is it a good practice to use it for other data types?
public class MergeSortExample
public Comparable[] mergeSort(Comparable[] inputList)
if(inputList.length <= 1)
return inputList;
Comparable[] list1 = new Comparable[inputList.length/2];
Comparable[] list2 = new Comparable[inputList.length - list1.length];
System.arraycopy(inputList, 0, list1, 0, list1.length);
System.arraycopy(inputList, list1.length, list2, 0, list2.length);
mergeSort(list1);
mergeSort(list2);
merge(list1, list2, inputList);
return inputList;
public void merge(Comparable[] list1, Comparable[] list2, Comparable[] resultList)
int indexOfList1 = 0;
int indexOfList2 = 0;
int indexOfMergedList = 0;
while(indexOfList1 < list1.length && indexOfList2 < list2.length)
if(list1[indexOfList1].compareTo(list2[indexOfList2]) < 0)
resultList[indexOfMergedList] = list1[indexOfList1];
indexOfList1++;
else
resultList[indexOfMergedList] = list2[indexOfList2];
indexOfList2++;
indexOfMergedList++;
System.arraycopy(list1, indexOfList1, resultList, indexOfMergedList, list1.length - indexOfList1);
System.arraycopy(list2, indexOfList2, resultList, indexOfMergedList, list2.length - indexOfList2);
java comparable
2
Wut exactly are you asking? When to use an Array instead of a list? Or when to use theComparable-Interface?
– Jere
Mar 23 at 18:29
No benefit, but there is certainly a drawback: it’s a raw type. Compile with all warnings turned on, and you will see it for yourself.
– VGR
Mar 23 at 18:31
The raw type warning is not from the array though, it is because Comparable is used instead of for example Comparable<SomeClass>. The use of raw types is a separate thing. Perhaps this is legacy code from before generics were introduced when all types were raw.
– ewramner
Mar 23 at 18:53
add a comment |
I was watching over a code which was using Comparable[] as an array that is exactly working as a collection framework list.As the task was done easily by using it without too many loops, is it a good practice to use it for other data types?
public class MergeSortExample
public Comparable[] mergeSort(Comparable[] inputList)
if(inputList.length <= 1)
return inputList;
Comparable[] list1 = new Comparable[inputList.length/2];
Comparable[] list2 = new Comparable[inputList.length - list1.length];
System.arraycopy(inputList, 0, list1, 0, list1.length);
System.arraycopy(inputList, list1.length, list2, 0, list2.length);
mergeSort(list1);
mergeSort(list2);
merge(list1, list2, inputList);
return inputList;
public void merge(Comparable[] list1, Comparable[] list2, Comparable[] resultList)
int indexOfList1 = 0;
int indexOfList2 = 0;
int indexOfMergedList = 0;
while(indexOfList1 < list1.length && indexOfList2 < list2.length)
if(list1[indexOfList1].compareTo(list2[indexOfList2]) < 0)
resultList[indexOfMergedList] = list1[indexOfList1];
indexOfList1++;
else
resultList[indexOfMergedList] = list2[indexOfList2];
indexOfList2++;
indexOfMergedList++;
System.arraycopy(list1, indexOfList1, resultList, indexOfMergedList, list1.length - indexOfList1);
System.arraycopy(list2, indexOfList2, resultList, indexOfMergedList, list2.length - indexOfList2);
java comparable
I was watching over a code which was using Comparable[] as an array that is exactly working as a collection framework list.As the task was done easily by using it without too many loops, is it a good practice to use it for other data types?
public class MergeSortExample
public Comparable[] mergeSort(Comparable[] inputList)
if(inputList.length <= 1)
return inputList;
Comparable[] list1 = new Comparable[inputList.length/2];
Comparable[] list2 = new Comparable[inputList.length - list1.length];
System.arraycopy(inputList, 0, list1, 0, list1.length);
System.arraycopy(inputList, list1.length, list2, 0, list2.length);
mergeSort(list1);
mergeSort(list2);
merge(list1, list2, inputList);
return inputList;
public void merge(Comparable[] list1, Comparable[] list2, Comparable[] resultList)
int indexOfList1 = 0;
int indexOfList2 = 0;
int indexOfMergedList = 0;
while(indexOfList1 < list1.length && indexOfList2 < list2.length)
if(list1[indexOfList1].compareTo(list2[indexOfList2]) < 0)
resultList[indexOfMergedList] = list1[indexOfList1];
indexOfList1++;
else
resultList[indexOfMergedList] = list2[indexOfList2];
indexOfList2++;
indexOfMergedList++;
System.arraycopy(list1, indexOfList1, resultList, indexOfMergedList, list1.length - indexOfList1);
System.arraycopy(list2, indexOfList2, resultList, indexOfMergedList, list2.length - indexOfList2);
java comparable
java comparable
edited Mar 23 at 19:06
Al-Amin
asked Mar 23 at 18:23
Al-AminAl-Amin
13
13
2
Wut exactly are you asking? When to use an Array instead of a list? Or when to use theComparable-Interface?
– Jere
Mar 23 at 18:29
No benefit, but there is certainly a drawback: it’s a raw type. Compile with all warnings turned on, and you will see it for yourself.
– VGR
Mar 23 at 18:31
The raw type warning is not from the array though, it is because Comparable is used instead of for example Comparable<SomeClass>. The use of raw types is a separate thing. Perhaps this is legacy code from before generics were introduced when all types were raw.
– ewramner
Mar 23 at 18:53
add a comment |
2
Wut exactly are you asking? When to use an Array instead of a list? Or when to use theComparable-Interface?
– Jere
Mar 23 at 18:29
No benefit, but there is certainly a drawback: it’s a raw type. Compile with all warnings turned on, and you will see it for yourself.
– VGR
Mar 23 at 18:31
The raw type warning is not from the array though, it is because Comparable is used instead of for example Comparable<SomeClass>. The use of raw types is a separate thing. Perhaps this is legacy code from before generics were introduced when all types were raw.
– ewramner
Mar 23 at 18:53
2
2
Wut exactly are you asking? When to use an Array instead of a list? Or when to use the
Comparable-Interface?– Jere
Mar 23 at 18:29
Wut exactly are you asking? When to use an Array instead of a list? Or when to use the
Comparable-Interface?– Jere
Mar 23 at 18:29
No benefit, but there is certainly a drawback: it’s a raw type. Compile with all warnings turned on, and you will see it for yourself.
– VGR
Mar 23 at 18:31
No benefit, but there is certainly a drawback: it’s a raw type. Compile with all warnings turned on, and you will see it for yourself.
– VGR
Mar 23 at 18:31
The raw type warning is not from the array though, it is because Comparable is used instead of for example Comparable<SomeClass>. The use of raw types is a separate thing. Perhaps this is legacy code from before generics were introduced when all types were raw.
– ewramner
Mar 23 at 18:53
The raw type warning is not from the array though, it is because Comparable is used instead of for example Comparable<SomeClass>. The use of raw types is a separate thing. Perhaps this is legacy code from before generics were introduced when all types were raw.
– ewramner
Mar 23 at 18:53
add a comment |
1 Answer
1
active
oldest
votes
In the old days before generics, collections could only use Object and were not type-safe. Arrays have always been type-safe. In those days it was common to use arrays for that reason. In modern code I would normally prefer to use a list in general-purpose code, but there is nothing wrong with arrays. The collections are implemented using arrays internally and sometimes an array is just what you want. Hard to give a better answer without seeing more of your code!
I got your point.I think it's too old practice to use comparable array that's why i could not find any resource about it.thanks
– Al-Amin
Mar 24 at 5:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317014%2fwhen-to-use-comparable-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the old days before generics, collections could only use Object and were not type-safe. Arrays have always been type-safe. In those days it was common to use arrays for that reason. In modern code I would normally prefer to use a list in general-purpose code, but there is nothing wrong with arrays. The collections are implemented using arrays internally and sometimes an array is just what you want. Hard to give a better answer without seeing more of your code!
I got your point.I think it's too old practice to use comparable array that's why i could not find any resource about it.thanks
– Al-Amin
Mar 24 at 5:24
add a comment |
In the old days before generics, collections could only use Object and were not type-safe. Arrays have always been type-safe. In those days it was common to use arrays for that reason. In modern code I would normally prefer to use a list in general-purpose code, but there is nothing wrong with arrays. The collections are implemented using arrays internally and sometimes an array is just what you want. Hard to give a better answer without seeing more of your code!
I got your point.I think it's too old practice to use comparable array that's why i could not find any resource about it.thanks
– Al-Amin
Mar 24 at 5:24
add a comment |
In the old days before generics, collections could only use Object and were not type-safe. Arrays have always been type-safe. In those days it was common to use arrays for that reason. In modern code I would normally prefer to use a list in general-purpose code, but there is nothing wrong with arrays. The collections are implemented using arrays internally and sometimes an array is just what you want. Hard to give a better answer without seeing more of your code!
In the old days before generics, collections could only use Object and were not type-safe. Arrays have always been type-safe. In those days it was common to use arrays for that reason. In modern code I would normally prefer to use a list in general-purpose code, but there is nothing wrong with arrays. The collections are implemented using arrays internally and sometimes an array is just what you want. Hard to give a better answer without seeing more of your code!
answered Mar 23 at 18:29
ewramnerewramner
4,01721226
4,01721226
I got your point.I think it's too old practice to use comparable array that's why i could not find any resource about it.thanks
– Al-Amin
Mar 24 at 5:24
add a comment |
I got your point.I think it's too old practice to use comparable array that's why i could not find any resource about it.thanks
– Al-Amin
Mar 24 at 5:24
I got your point.I think it's too old practice to use comparable array that's why i could not find any resource about it.thanks
– Al-Amin
Mar 24 at 5:24
I got your point.I think it's too old practice to use comparable array that's why i could not find any resource about it.thanks
– Al-Amin
Mar 24 at 5:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317014%2fwhen-to-use-comparable-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Wut exactly are you asking? When to use an Array instead of a list? Or when to use the
Comparable-Interface?– Jere
Mar 23 at 18:29
No benefit, but there is certainly a drawback: it’s a raw type. Compile with all warnings turned on, and you will see it for yourself.
– VGR
Mar 23 at 18:31
The raw type warning is not from the array though, it is because Comparable is used instead of for example Comparable<SomeClass>. The use of raw types is a separate thing. Perhaps this is legacy code from before generics were introduced when all types were raw.
– ewramner
Mar 23 at 18:53