Question

Java - how to get the minimum distance between array list elements? Code.

Java - how to get the minimum distance between array list elements? Code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;

//TestCode.java
class TestCode {
    public static int getMinDistance(ArrayList<Integer> list){
        int dist = Integer.MAX_VALUE;
        for(int i = 0;i<list.size();i++){
            for(int j = 0;j<list.size();j++){
                if(i!=j){
                    if(dist > Math.abs(list.get(i)-list.get(j))){
                        dist = Math.abs(list.get(i)-list.get(j));
                    }
                }
            }
        }
        return dist;
    }

    public static void main(String [] args){
        ArrayList<Integer> list = new ArrayList<>();
        list.add(35);
        list.add(67);
        list.add(4435);
        list.add(445);
        list.add(485);
        list.add(25);

        //35-25 = 10
        System.out.println("Minimum distance = "+ getMinDistance(list));
    }
}

\color{blue}Please\; up\;vote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
Java - how to get the minimum distance between array list elements? Code.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please code in java and add code... in an array containing 10 numbers, how to find...

    Please code in java and add code... in an array containing 10 numbers, how to find the maximum and minimum values?

  • JAVA When a two dimensional array of data type boolean get instantiated, the array elements are...

    JAVA When a two dimensional array of data type boolean get instantiated, the array elements are given a default value of true false 0 null QUESTION 2 How do you declare an ArrayList object reference of Book objects named books? O ArrayList books O ArrayList<> books ArrayListBook books ArrayList books Book

  • Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm...

    Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm Write a static function called RemoveNullElements() without creating another array. that remove Null elements and shift all the rest of the elements to the left/front (small indexes) of the array without changing the size or length of the original array( this meant the front of the array will have all the not Null elements, and the other haft will be empty/Null. Please provide test...

  • Need help with my Java Hw: Consider an algorithm that sorts an array of n elements...

    Need help with my Java Hw: Consider an algorithm that sorts an array of n elements by finding the smallest and largest elements and then exchanges those elements with the elements in the first and last positions in the array. Then the size of the array is reduced by two elements after excluding the two elements that are already in the proper positions, and the process is repeated on the remaining part of the array until the entire array is...

  • 23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a...

    23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a second oversize array with size2 elements, write a method that returns the first array with the elements of the second appended to the end. If the capacity of the oversize array is not large enough to append all of the elements, append as many as will fit. Hint: Do not construct a new array. Instead, modify the contents of the oversize array inside the...

  • Java code: Use at least one array defined in your code and one array or array...

    Java code: Use at least one array defined in your code and one array or array list defined by user input.    Examples of arrays you can define in code are: A deck of cards, the days of the week, the months of a year. User input can be things like grades, shopping list items, wish lists, deposits an withdrawals, etc. Be able to add, remove, print, sort as necessary to complete the program's goal. Thanks

  • (Java) how to sort descending order in an array list [of float].

    (Java) how to sort descending order in an array list [of float].

  • Please complete this code for java Lab Write a program as follows: Create an array with...

    Please complete this code for java Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a 'match found' message. Also print at which index the match was found, else print a 'match not found' message...

  • Write the java code for printing a two dimensional array. You may assume any number of...

    Write the java code for printing a two dimensional array. You may assume any number of row and columns for this array. Just declare the array and write the code to print the elements row by row.

  • C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists...

    C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists of any primitive type you want. (Array of size 2020) (This could be based on MSDN libraries or the lab) – you do not need to instantiate any of the linked lists to contain any actual values. Paste your code for that here (this should only be one line) Based on your code or the lab from 4 or your doubly linked list from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT