Skip to main content

#SortingArrayElement #sorting #ArraySorting #erprince SORT THE ARRAY EL...



Java interview Question to sorting the Array element's



See you are new to java or i might have suggested you to use Hash Map or other Collection classes.

So lets forget it but look don't be in hurry to print the elements first sort them.
And as you are having its name associated with it you have to also take care of the same.

So first find the number of lines in the file and then allocate that much space to array but for 2 arrays
take 1 array for storing names
another for storing values
suppose you got number of element in variable size so
String[] name = new String[size];
int[] value = new int[size];
This will save memory or else you can set anything default


Then one by one pick it from file and place it by splitting.
But remember you will get values in String so you have to parse to get int
here it goes
get is a string array
value[i] = Integer.parseInt(get[i]);
value[i] = Integer.parseInt(get[i].trim());// If it wont work then string might be not trimmed.
After that check for bubble or any sorting technique to rearrange them by number
String temps = "";
int tmpi = 0;
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size; j++) {
if (value[i] < value[j]) {
//Just swap values
tempi = value[i];
value[i] = value[j];
value[j] = tempi;

//Now also swap the respective names
temps = name[i];
name[i] = name[j];
name[j] = temps;
}//End if
}//end j loop
}//end i loop

Now print them...

Its so easy isn't it.
hello_everybody · 9 years ago
1Thumbs up 0Thumbs downReport AbuseComment
Ask a Question
usually answered within minutes!
What's up?
Details
Suggested Searches
Backup Restore
Internet
Firewall
Laptops Discounted
Voip Phone Systems

Related Questions
Help me to write a program to sort using sort() function in JAVA ?
There is a method for sorting in java(Collection.sort) then why we use various sorting algorithms in java?
Why java api uses different sorting algorithms?
Java bubble sort using recursion?
How can you sort characters using insertion sort in java?
Answer Questions
How can we get teachers to have better lives without changing the tax code?
How do I do a while loop in c++ saying if num1 != 2 and num2 != 2, run? I tried while ( num1 !=2 && num 2 !=2); But it doesn't work.?
Why wont my python code work?
Python Coding Question?
Trending
I don t understand what we're doing in class. ICT What is problem solving?
5 answers
Working in warehouse?
9 answers
Im 19. Am I to old to start learning how to code now?
27 answers
I'm thinking of hiring a company to develop an app for me, how can I make sure my idea is not stolen?
6 answers
How do you change the deviantart interface to the 2017 revamped interface?
3 answers
I need help creating a batch file on notepad ++ asking users what they want to do with folders!?
4 answers
TermsPrivacyRSS

Comments

Popular posts from this blog

How to Send OTP in Mobile Number | login with OTP mobile Number | How to send OTP in mobile no with Spring Boot APP

   ðŸ˜‚               Login with Mobile Number OTP ---------------------------------------------------------------------------- If you want to develop a project to log in with OTP mobile Number in Spring Boot Applications then this post for you. In this post, I am going to use some other service to send the OTP in mobile number. we have to use it in this project spring boot. we are going to use Twilio to send SMS. we are going to use a web socket to send the data from the browser to the SMS gateway. Oracle Database for store the user details. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.3.3.RELEASE</version> </dependency> <dependency> <groupId>com.twilio.sdk</grou

Spring Boot With MySQL Database connection with Examples | MySQl Database Configuration with Spring Boot Projects

 ðŸ˜ƒ MySQL Database Configuration with Spring Boot Projects  In this article, we are going to introduce How to connect MySQL Database with the Spring Boot project. pom.xml   <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</gro

How can we create Auto generated field or ID for mongodb using spring boot

😂 How can we create an Auto-generated field or ID for MongoDB using spring boot? First Create One Application Like Mongodb_sequence-id-generator Pom.XML <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>de.flapdoodle.embed</groupId> <artifactId>de.flapdoodle.embed.mongo</artifactId> </dependency> User.java package com.app; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "users_db") public class User { @Transient public static final String SEQUENCE_NAME = &