Need to sort Arrays [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to code a voting system, with candidate names and input.
Each voter will be given a screen with JTextfields to score each candidate, each score goiing into an array where i is the candidate number and score[i] is the candidate score.
I am having trouble sorting the arrays from highest score to lowest whilst being able to see which candidate has which score and then being able to use that to display the winner.
I have seen bubble sorting but that does not always sort it exactly from high to low and i've also seen the Arrays.sort(ArrayName); method.
Can anyone help?

It sounds like you're using parallel arrays, and if so, stop. Instead move away from the GUI program for a minute and instead create some decent OOP compliant classes, including a class for Candidate which holds the score and candidateNumber data. Then it would be easy to sort your collection (such as an ArrayList<Candidate>) using Collections.sort(...). If your Candidate class implements the Comparable<Candidate> interface, then your job gets even easier still since no Comparitor class would be needed. Note that if you have the class implement Comparable, you'll need to give it a public int compareTo(Candidate o) method that will return an int, 1, 0, or -1, depending on the score of the o Candidate vs. this Candidate's score.

Related

Are classes with identical values .equals()? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm using Point3D.
I have a list full of Point3Ds. I need to create a NEW Point3D at some point, and check if the list contains it.
Will this list ever contain it as it is technically a different reference/object BUT has identical values?
I understand this is a lack of fundamental knowledge in my Java.
A List doesn't care what you put in it - put ten things in, get ten things out.
A Set however will generally only have unique values in it (using .equals() and hashCode() and in some sets other things - e.g. Comparator).
Normally I'd say "to the javadoc" however in this case it has a case of cut/paste'ism from Point3D JavaDoc
equals
public boolean equals(Object obj)
Returns a hash code value for the point.
Overrides:
equals in class Object
Returns:
a hash code value for the point.
So assuming it does what most people expect if you have two points generated with identically valued doubles then yes it'll be as you expect.
HOWEVER, floating point numbers you can easily be "very very close" when you expect to be identical (due to the representation of the value as FP), and remember there are 3 doubles in a Point3D [so 3 potential lots of small error] - so to be safe typically you might decide things are the same within some small distance see javadoc for distance rather than relying on exact matching.

I need 2 denominational array sorted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am working on a project that has a list of student name and numbers, for example
James Bloggs,1
Paul Jonson,43
Andt Peters,23
Once I have them in an array I then need them sorted.
What is the best way of going about this. Its not the sort Im stuck on its the referencing the names to the numbers. I would have thought if I do a 2 denominational array only one would be sorted.
Any help would be great,
EDIT: I just realized this question was asking about a 2-dimensional array and my answer doesn't directly deal with that. I am skeptical that arrays should be involved at all. Arrays are usually for dealing with primitive data, and maybe if you are coming from a C background you'd think they'd be the natural thing to use. If you really honestly have to use arrays then this probably isn't the way to go.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
public void foo(){
// Use a TreeMap. It will sort keys on insertion.
Map<Integer,String> nameByNumber = new TreeMap<>();
nameByNumber.put(1, "James Boggs");
// etc. put all the entries in however you need to
List<Integer> sortedNumbers = personByNumber.getKeys();
List<String> namesSortedByNumber = personByNumber.getNames();
}
If you need it to be more organized and complex, you can encapsulate the name and number into a Class with a name and number property. Then you'd still use the number as the key, but you'd have the full class as the value. Do this if you need to have more than just a name, like last name, first name, address, etc.

get subset of elements from set in circular way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Consider a set with like 100 elements.
Set<String> originalSet; //[1....100] size is 100
From originalSet, (m) subset of elements of some size(n) with some starting index(i) have to retrieved.
Example:
m = 4, n = 45, i = 1
Following have to be retrieved
subset1[1-45], subset2[46-90], subset3[91-35], subset4[36-80]
Whats the best way of doing this.
First of all, Set is unordered so it does not make sense to talk about indices etc. List would make more sense here.
Next, you have to be explicit about what you mean by "best". Performance on insertion? Random access? Creation of your n-from-i subset? These are important question to choose the implementation.
I think two primary options would be linked list with special handling of the last element in subList operation or an array-based list.
Assuming your set has some notion of order, you could write this with
Iterable<Iterable<String>> slices =
Iterables.limit(
Iterables.partition(
Iterables.skip(
Iterables‌​.cycle(originalSet),
i),
n),
m);
If you wanted a set out of this, you'd have to do a transform or something; if you have Java 8 that'd just be something like Iterables.transform(..., ImmutableSet::copyOf).

Take a result of two long numbers using an array in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm

Increasing Probability for generating specific random numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to generate random numbers . Now, I want to generate a new set of random numbers in a way that that increases the probability of returning the numbers that were generated earlier.What is the best way of achieving this ?
I dont know if predefined functions exist or not, but I have something in mind that you can try.
Make an ArrayList of the numbers. (eg. if you want to generate from
0to30, then make an ArrayList of Integers from 0 to 30).
Shuffle the ArrayList(Collections.shuffle())
Pick the first 2 numbers.
Then, just add those 2 numbers into the ArrayList as many times as you want the probablity to shift towards them.
Shuffle again and pick
You can store that values on array.
And you can have a random condition also for having a new pair of number or pick randomly on your array.

Categories

Resources