Question

how do you add elements to an array when they enter the array the array has...

how do you add elements to an array when they enter the array the array has to be sorted and you can only use the compareto method to sort NOTE:/// you cannot use sort.array() you can only use the compareto method to sort the element and show the tested version here and use java please thanks
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

public class Test {
public static void main(String []args) {
   String str[] = { "Ajeet", "Steve", "Rick", "Becky", "Mohan"};
   String temp;
   System.out.println("Strings in sorted order:");
   for (int j = 0; j < str.length; j++) {
    for (int i = j + 1; i < str.length; i++) {
       // comparing adjacent strings
       if (str[i].compareTo(str[j]) < 0) {
           temp = str[j];
           str[j] = str[i];
           str[i] = temp;
       }
   }
   System.out.println(str[j]);
   }
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
how do you add elements to an array when they enter the array the array has...
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
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