im trying to create an insertion sort method for a vector. I know how to insertionsort for an array, but for a vector im having problems
Source code:
PHP Code: package test;
import java.util.*;
import java.io.*;
public class LinearSearch {
public static void main (String[] args) {
Vector myVector = new Vector();
[Code]...
I'm getting errors at lines 38 and 39 "Left-hand side of an assignment must be a variable". "Syntax-error(s) on token(s) misplaces contructor(s)". How can i fix them ??
Im trying to do an insertion sort using ArrayLists and I keep getting this error after the sorting section where it doesnt sort anything at all, but still displays my previous array list.:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 7 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Utilities.insertionSort(Utilities.java:102) at Utilities.main(Utilities.java:66)
My code:
import java.util.*; import java.lang.*; public class Utilities { public static void main(String[] args) { ArrayList<String> equipment = new ArrayList<String>();
I could have copied the code for a standard algorithm such as insertion sort, but I wanted to do it on my own to see how well I think. I came up with a working solution below. This is efficient or not or if I can make improvements. Would this approach ring any alarm bells in an interview ?
public static void insertionSort(int[] unsortedArray) { int[] a = unsortedArray;// alias for the array int s = 0;// index before which all elements are in order. int tmp = 0;// temporary variable int last = a.length - 1;
I can't spot where my java implementation of insertion sort differs from the pseudocode here:Well, there is one difference in the parameters used by the insert method, which is inconsistent in the pseudocode.I'm pretty sure it should be calling insert (a,n) instead of (a,i,n);
insert(a,k) i←k x ← a[k] while x < a[i − 1] x ← a[i] a[i ] ← a[i − 1] i ←i −1 a[i]←x return
insertion-sort(a,n) m ← select-min(a,1,n) swap(a,1,m) fori from2upton insert(a,i,n) return
Here is my attempt at a java implementation, which doesn't actually seem to do anything.I've kept variable names as in the pseudocode. Might technically be bad practice, butI think it should make it easier to follow in this particular scenario.public class InsertionSort {
Why am I so interested in this pseudocode when there are simpler java-ready examples of insertion sort on the internet? Simply because this is the code the professor uses, so I should be able to understand it.
I am working on my generic insertion sort program. When I completed and run my code, I am having trouble with my code. So, I am just trying to see if I get an correct array from a file. However, i just get so many nulls in the array. Therefore, I can't run my insertionSort function because of null values.
I have to write the Insertion Sort Algorithm using Java codes and at the same time find the time of execution for different sizes of array, filled with random numbers. If I try to show the numbers inserted into the array randomly, they don't appear at the console.
import javax.swing.JOptionPane; public class Insertion { public static void main(String[]args){ int SizeArr = new Integer(JOptionPane.showInputDialog("Enter the size of teh array")).intValue(); int [] r= new int [SizeArr]; {for(int d=0; d<r.length; d++)
class GVector { // TODO: declare a private array to save the vector coordinates // Creates a mathematical vector of d dimensions, initialized at 0 public GVector(int d) { // TODO: implementation
[Code] ....
I'm confused with what type of array I need to use to save the vector coordinates and what to put in Gvector. Is it a constructor?
when I am programming let say in VB using Visual Studio, finally I build .exe file that can be run on all Windows by double click.For Java I am using Eclipse and to run those apps I am using run from Eclipse.How I can create a sort of "executable" for my Java app that I would be able to run it by file click on Windows or Linux?
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;
I am getting incombatable types, I do not know why I am getting them..why I am getting the error?
The Error I am getting: stringSort.java:26: error: incompatible types if(myArray[j].compareToIgnoreCase(myArray[i].toString())){ ^ required: boolean found: int */
However, whenever I run the method, the element that should go last, Zachary, in this case, ends up getting moved to the front for some reason. I'm not sure why.
I tried changing what the first element was initialized to, to the variable i as that would logically work as well, but it ends up missing the first element in the list.
Java Code:
public static void selectionStringAscendingSort (String[] words){ int i, j, first; String temp; for ( i = 1; i < words.length; i++ ) { first = 0; //initialize to subscript of first element for(j = i; j < words.length; j ++){ //locate smallest element between positions 1 and i. if( words[ j ].compareTo(words[ first ]) <0 ) first = j; } temp = words[ first ]; //swap smallest found with element in position i. words[ first ] = words[ i ]; words[ i ] = temp; System.out.println(Arrays.toString(words)); } System.out.println(Arrays.toString(words)); } mh_sh_highlight_all('java');
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?
I have an ArrayList, based on the class which stores cricket players, their names and runs scored.When I use the Collections.sort() method my arraylist is sorted alphabetically by forename.how to OverRide the comparing method to sort by runs, and thus the code I use to sort the list?
I'm doing an exercise we're you're supposed to sort strings in alphabetical order, without importing anything , not using the Arrays.sort() method.
I think I got the method down partially right, or it is on the right track, but it is completely not being applied to my answer. All it prints out in the console is the actual String array twice, without sorting anything.
public class arrayofstrings { public static void sort(String[] a) { String temp= ""; int min; int i= 0; for (int j=0; j<a.length-1; j++) {
import java.util.ArrayList; import java.util.Random; public class NumSorting { public static int numOfComps = 0, numOfSwaps = 0; public static void main(String[] args)
[Code] ....
What is wrong with my code in this sorting program? It won't copy the random generated numbers to the sort method. How to get the random generated numbers to copy to the sort method. Below is what the program is displaying.
So I'm trying to implement a quick sort method for an ArrayList of Strings and right now I'm getting the compiler error message: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space. I don't know what that error means nor how to fix it. I've marked in my code where the error seems to be occurring.
import java.util.ArrayList; public class quickSort { // constructor public quickSort()
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 [].
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();
I'm having some issues, trying to solve this problem in java. I want to print some election results, and i have to loop through a vector of objects and retrieve the partial sums of each party's seats for each constituency and the national results for each party. For now i can print the results per contituency, but i'm having problems in getting the national results. Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.
This is what i have.
Java Code:
while (i < h.geral.size()) { show += "Constituency - " + ((Party) h.geral.elementAt(i)).getConstituency() + "
I understand how vectors work I'm currently using one to store my id's from my txt file but how do you put them in a defaultComboBoxModel?
DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(); //declare a vector object that will hold a string (only thing that works with comboboxmodel Vector<String> myVector=new Vector<String>(); //try statement try{ FileReader fr = new FileReader(file);
[Code]...
Any example of how a vector is used with a defaultComboBoxModel so I can then use that to populate my JComboBox?
When I input "the" or "and" I always receive "its not here" as the output. Why is this? I also tried printing the values of v.get(i).other and the system printed null twice.
public class Translator { public String other; public Translator(String x) { x = other;