Sort Both Array List
Jun 29, 2014
Directions: public static void initialize(ArrayList names, ArrayList sores)
You should write a function that sorts both array lists, based on the values in the scores array list. This is one of the more conceptually challenging parts of the assignment. You want to sort the scores array list, and simultaneously alter the names array list so that the names continue to line up with their respective scores by index. In the example data above, when the score 9900 moves to the first element of the scores array list, the name "Kim" should also be moved to the top of the names array list. The function should have the following signature:
I'm having trouble figuring out how to sort the lists.
import java.util.ArrayList;
import java.util.Scanner;
public class Assignment5
{
/**
*/
public static void main(String[]args) {
intializeArrays();
[Code] ....
View Replies
ADVERTISEMENT
Nov 23, 2014
I am having an issue trying to print different types of arrays using one method. Eclipse tells me to convert my print method for each array type:
change method 'printList <e>' to 'printList(ArrayList<integer>)'
change method 'printList <e>' to 'printList(ArrayList<Double>)'
change method 'printList <e>' to 'printList(ArrayList<Character>)'
change method 'printList <e>' to 'printList(ArrayList<String>)'
I would like to accomplish printing all four different arrays using one print method. How to accomplish it or provide examples or links to examples?
import java.util.ArrayList;
public class SortArrayList {
public static class Sort {
[Code].....
View Replies
View Related
Feb 14, 2014
I have some class called sorted to sort the linked list through the nodes of the list. and other class to test this ability, i made object of the sort class called "list1" and insert the values to the linked list.
If i make other object called "list2" and want to merge those two lists by using method merge in sort class. And wrote code of
list1.merge(list2);
How can the merge method in sort class know the values of list1 that called it as this object is created in other class.
View Replies
View Related
Mar 26, 2014
Add accounts on a list, each account contain: name, accountCode, pinCode, balance.
How to show list sort by balance?
View Replies
View Related
Oct 30, 2014
I searched a lot but can't seem to understand the sorting of a SLLNode... I noticed a method called Bubble Sort, I understand how it works, but can't think of a way to implement it to my code..
View Replies
View Related
May 5, 2015
I am learning to use heaps and as an exercise I am trying to write a program using a heap class I have created to sort words. I have read in words from a file and added them to the heap successfully. I am having some trouble figuring out how I can print out a sorted list of the words. From my understanding of how a min-heap works, if I remove from the min/root node they will always be removed in sorted order. So far I have tried out to do a simple for loop but, only half of the heap is being removed. Not sure if my logic is incorrect of there is an error somewhere in my removeMin() function specifically in the while loop.
public static void main(String[] args) {
Heap heap = new Heap();
heap = read( heap );
for( int i = 0; i < heap.getSize(); i++){
heap.removeMin();
[Code] ....
View Replies
View Related
Dec 11, 2014
I've playing around with linked lists and methods for sorting. So far I've tested the iterative sort, insertion sort, quick sort and they all worked perfectly. Now, I am trying to implement merge sort that would take a linked list of jobs and sort them according to their priority. I found a few solutions on the web, of which I am trying to implemented this one: LeetCode.
My system is a simple one, I do have a linked list of print jobs, each of which has the priority. The following code should sort my print queue and return the link node of the first sorted element. Here's the code.
//defining a job that has priority
public class Job {
private int priority;
[Code]....
The problem I've been trying to solve is that I am getting the stack overflow at line
ListNode<T> h1 = mergeSort(left);
meaning that I am getting into a loop somewhere down through the process of breaking the linked list into half, half or halfs and so on.
View Replies
View Related
May 22, 2014
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.lang.Comparable;
import java.util.*;
import java.lang.*;
public class MyLinkedListSort {
[Code] .....
Output is :
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0
Expected output :
Name is Crish Salary: 2000
Name is Tom Salary: 2400
Name is Ram Salary: 3000
Name is John Salary: 6000
Where i going wrong
View Replies
View Related
Oct 6, 2014
I'm having trouble with sorting Strings- 3 strings inputted by user, and I would like to output them in alphabetical order. I've used the str.compareToIgnoreCase method, and then I've tried to loop them through a series of if/ else statements. Everything I've been able to find online (including the forums here) has suggested to use the Comparator class, or to put the strings into an array, and sort list- I really would like to stick with just the String class, and its methods .
The program itself works and compiles, but I am getting logic errors that I have been unable to solve. I'm using IntelliJ Idea, and I've ran it through the built in debugger, about 100+ times (not exaggerating, lol) just to see what it's doing in particular scenarios. For instance, I can get c, a, b, to print out as a,b,c correctly, but a,b,c, will print out as b,a,c.
For me this is kind of like a Sudoku puzzle, or a Rubik's cube! Each time I fix one scenario, it breaks another one, so I don't know if there's a(logic) solution to fix all possible scenarios (abc, acb, bac etc... to all print abc) or if possibly I just need more if statements. I've only pasted in the area where I'm having problems (the if statements). I'm a big fan of the "Next Line" syntax.
(Note: please assume the non relevant content- import Scanner class, main method, etc... I didn't want to paste the entire program.)
System.out.println("Enter the first statement: ");
//input.nextLine();
string1 = input.nextLine();
System.out.println("Enter the second statement: ");
string2 = input.nextLine();
System.out.println("Enter the third statement: ");
string3 = input.nextLine();
[Code] ....
View Replies
View Related
Jan 22, 2015
how can I got about sorting an array that contains more than one value in a single element. Such as my array below has 4 values under one element. I know how to sort elements with single values however, slightly confused on this.
import java.util.Scanner;
import java.util.Arrays;
class Mobile
{
[Code]......
View Replies
View Related
Jul 8, 2014
I wrote this piece of code, and it is supposed to use bubblesort to sort the array. Yet somehow it re-aranges it, but doesn't exactly sort it. For example, when I enter [3, 5, 6, 9, 8, 4, 7, 5, 6, 2], it prints Done [2, 5, 9, 5, 7, 8, 4, 6, 3, 6]
Java Code:
import java.lang.*;
import java.util.*;
import java.util.Arrays;
public class bubble {
public static int[] array(){
int [] anArray = new int [10];
[Code]...
View Replies
View Related
Dec 15, 2014
How I would sort two heaps into one array. I know that in a heap the "parent" is guaranteed to be larger than the "children".
So could I simultaneously traverse both trees in order, and while doing so, store the values of each node, one after another? This way the array would be partially sorted and then I could just use an insertion sort.
Is this logic reasonable or am I looking at it the wrong way?
View Replies
View Related
Mar 30, 2015
I am writing a program which writes down all possible equation y=a+b+c values from min to max (in reality this equation would more difficult, but here is just short example).
The problem is that my sorting code can't get access to full array in loop.
Is there any way to pass array to sorting code, or somehow change sorting code?
package pkg06;
public class Main {
public static void main(String[] args) {
double aS =-1;
double aE = 3;
[code]...
View Replies
View Related
May 21, 2014
This is a lab for one of my CS classes, and the assignment is to create a randomly filled array (values 10-100) and use these values as the height of an array of rectangles (essentially a bar graph)that will be drawn on a page. After that's done, the code should use the selection sort method to sort the bars least to greatest, being repainted as it's sorted.
I'm receiving no errors, the original draws just fine, and the code sorts the first position and then...it just hangs. Like it's in an infinite loop but I have all of the modifiers in place (I think. I've been staring at this code for three days straight and I don't think I really see it anymore). I've tried talking to my professor and I get that her private life is really busy right now, but she just keeps blowing me off and I don't know what to do. Anyway, done with back story and whining so here's the code.
Rectangle class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rectangle
[code]....
View Replies
View Related
Nov 11, 2014
I have a question about selection sort. If I had an array of numbers, 1 2 3 4 100 0, and used the selection sort method (below) on the array,would the 0 slowly work it's way down to the end?
Nest loop = full rotation of nest loop
Nest loop 1: 1 2 3 4 100 0
Nest loop 2: 1 2 3 4 100 0
Nest loop 3: 1 2 3 4 100 0
Nest loop 4: 1 2 3 4 100 0
Nest loop 5: 1 2 3 4 0 100
Nest loop 6: 1 2 3 0 4 100
etc.
METHOD:
static void selectionSort(int[] arr){
int n = arr.length;
for(int i = 0;i<n;i++){
int min = i;
for(int j = i+1;j<n;j++){
if(arr[j]<arr[min]){
min = j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
View Replies
View Related
Jun 15, 2014
Merge sort implementation on the given array i listed in the code below. I know Arrays.sort() or Collections.sort() could do the trick but i want to achieve it through merge sort.
import java.util.*;
public class Merge{
public static void main(String[] args){
String[] myStringArray=new String[]{"z","a","z","b","c","e","z","f"
,"g","a","w","d","m" ,"x","a","R"
,"q","r","y","w","j","a","v","z","b"};
}
}
View Replies
View Related
Feb 19, 2014
Code below. I am not sure if my logic is correct. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.
import java.util.Scanner;
class regnumber
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("registration number ", 100,1000);
[Code] ....
View Replies
View Related
Jan 10, 2015
I am having trouble with an array out of bounds exception. I understand what the error means (that I am trying to access part of array that does not exist). But I do not see the error in my code.
Java Code:
public class bubbleSort
{
public static void main(String []args)
{
int i;
int array [] = {12,9,4,99,120,1,3,10};
[code]....
View Replies
View Related
Feb 28, 2015
I have read through some of the posts on insert sort /arrays and see close but not exactly what I'm looking for. I don't want to use a library to do work for me, I want to do it myself and understand it.
OrderedArrayList.java
// import static org.junit.Assert.assertEquals;
public class OrderedArrayList<T extends Comparable<T>> {
// T is a variable that contains a class type, array of T things
private T[] arrayA; // changed to private per example
private T[] arrayB;
int numElements = 0;// number of elements full (not null), used in multiple methods. numElement -1 = back
[Code] ....
View Replies
View Related
Nov 19, 2014
I know it is possible to do this using the Array class but I don't want to use that. I am trying to use selection or bubble sort but I believe selection is what I want. This program asks the user to input 2 arrays with 10 integers each, sorts them from smallest to largest, then states whether they are the same or not. Comparing the two arrays.
import java.util.Scanner;
public class SetDifference
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int array1[] = new int[10];
int array2[] = new int[10];
[Code] .....
View Replies
View Related
Nov 27, 2014
I working on a bank program that gets a user id, name and balance from a text file. I need to be able to sort based on each field. I'm using Collection.sort and Comparator. I'm using String[] array = file.toFile().list(); to get the values for the array from the text file. I can't figure out how to make this into an arraylist. I understand the concept when hardcoding the data but having trouble doing it from a text file.
View Replies
View Related
Oct 22, 2014
Use the sort method of the arrays class to sort the values in the array, and print the median value(the 50th value) on the console followed by a blank line. Then, test this enhancement. Print the 9th value of the array on the console and every 9th value after that. Then, test this enhancement.
import java.util.Arrays;
public class ArrayTestApp
{
public static void main(String[] args) {
double [] arrayTest = new double[99];
//adding random number to each element in the array
for(int i=0; i<arrayTest.length; i++)
arrayTest[i] = 100.0*Math.random();
[Code] ....
OUTPUT
run:
The average is: 49.842845462514944
The median is: 49.68753724038633
The 9th value is: 2.599530043466969
The 9th value is: 11.486193141397095
The 9th value is: 20.14206270200648
[Code] ....
View Replies
View Related
Jun 30, 2014
I'm supposed to write a program, which reads float numbers from a file, puts them in an array and sorts the array. After that I'm suppose to add the numbers so that when I add the 1 and 2 number of the array, I'm suppose to save the sum on the position of number 1, then I add number 3 and 4 and save the sum on position 2 etc. Also if my array has an uneven number of floats it's suppose to add the last 3 and not 2 numbers in the last iteration. The problem is the method throws an ArrayOutOfBounds Exception but I can't seem to find my mistake.
That's the second method. The first one just stores the sum in another variable and then returns it. Also is there a way in that I can Scanner/File/array etc. and initialize the array only once so I don't write the same code two times like it is now.
package sumOfFloats;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class SumOfFloats {
public static float sumFloats() throws FileNotFoundException{
[Code[ ....
View Replies
View Related
Dec 7, 2014
I have an array that I filled with 30 random characters, but now I am trying to sort them in ascending order and the descending order using lambda expressions.
public class RandomCharacters {
public static void main(String args[]){
Random r =new Random();
char myarray[] = new char [30];
for (int i = 0 ; i < 30; i++)
[Code] ......
View Replies
View Related
Jun 13, 2014
How to input 10 integer elements in an array & sort them in desc. order using the bubble sort technique.
View Replies
View Related
Nov 20, 2014
This time I am having difficulties with selection sort method, but not with the method itself (I think). So I need to sort an array and return the result of each step.
This is the main code:
public class Functionality {
public static int[][] selctionsort(int[] a) {
for (int i=0; i<a.length; i++) {
int least = i;
for (int j=i+1; j<a.length; j++)
[Code] ....
And this is the Test folder:
import static org.junit.Assert.assertArrayEquals;
public class PublicTests {
public static void main(String[] args) {
int[] a = new int[] { 35, 7, 63, 42, 24, 21 };
int[][] b = new int[][] { new int[] { 7, 35, 63, 42, 24, 21 },
[Code] ....
Now I am not sure what should I write in return since the 2nd (test) project has int[][] and in my main project I am working with int [].
View Replies
View Related