我正在学习Java并编写QuickSort类的代码。在某种程度上,需要交换数组中的元素,所以我尝试使用Collections.swap来实现这一点,例如,推荐使用。但是,javac QuickSort.java向我抛出一个错误:
error: swap(Object[],int,int) has private access in Collections
我在这里做错什么了?QuickSort.java完整代码
package src.sort;
import java.util.Collections;
public class QuickSort {
public static
我有两个Runnable类,Reader和Writer。
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.concurrent.Exchanger;
public class Reader implements Runnable {
private static final int THRESHHOLD = 1000;
private final int START, STOP;
private Excha
package codeabb;
import java.util.*;
public class Bsort {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int arr[] = new int[100];
int swap = 0;
int pass = num-1;
for( int i = 0; i < num
我已经编写了一个方法,它对arrayList进行排序,并交换该arrayList中的连续元素。我面临一个问题-
如果我使用Collections.sort()实现该方法,则会产生堆大小错误,而如果使用Arrays.sort(),则不会给出结果并成功运行。
public ArrayList<Integer> sortAndSwap(ArrayList<Integer> a) {
Collections.sort(a);
for(int i = 0; i < a.size()-1; i+=2) {
int temp
我在根据病情和到达时间对病人名单进行排序方面有问题。能看看我的代码有什么问题吗?看到LList.java,SortPatient方法是正确的吗?
ListInterface.java
public interface ListInterface<T> {
public boolean add(T newEntry);
public boolean add(int newPosition, T newEntry);
public T remove(int givenPosition);
public void clear();
public boolean replace(
我尝试了许多不同的变体,但我总是遇到同样的问题。在selectio nsort运行后,输出的项数与我的数组大小不匹配。我已经遍历了任何大小为10的数组,但是输出没有包含10个数字。但是,选择排序的输出是排序的。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Sorts {
public static Integer[] createArray(int size) {
List<Integer> list = new ArrayLi
我刚开始学习Java,我有以下任务:
编写一个程序,从键盘读取三个整数a、b和c,并交换三个值中最大和最小的位置。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Program to find the largest and smalles value");
Syste
我遵循以下伪码:
function quicksort(array)
if length(array) > 1
pivot := select any element of array
left := first index of array
right := last index of array
while left ≤ right
while array[left] < pivot
left := left + 1
while
我编写了一个代码,它应该显示数组中元素排列的全部组合。
package com.company;
import java.util.ArrayList;
import java.lang.Math;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
first(3);
}
static int factorial(int n) {
int res = 1
这段Python代码也可以用Java代码编写吗?
def gcd(a, b):
# Return greatest common divisor using Euclid's Algorithm.
while b:
a, b = b, a % b
return a
print (gcd(210, 45))
这是我到目前为止在Java代码中所做的:
private static int gcd(int p, int q) {
// Return greatest common divisor using Euclid's Algo
日安!我这里有一个Java程序,它可以进行快速排序。它读取一个文件,然后对其中的前10000字进行排序。我在介绍算法时遵循了托马斯·科门的伪码,第二版。
import java.io.*;
import java.util.*;
public class SortingAnalysis {
public static int partition(String[] A, int p, int r) {
String x = A[r];
int i = p-1;
for (int j=p; j < r-1; j++) {
public class JavaApplication28 {
public static void main(String[] args) {
int arr[]={5,4,3,2,1};
int n=arr.length;
int d=0;
for(int i=1;i<n;i++){
d=i;
while(d>0 && arr[d-1]>arr[d]){
d--;
int temp=arr[d-1];
我的Java应用程序在JIT编译期间工作了几天之后就失败了。可能是本机内存碎片化,但它只需要8Kb .我有700多条线。2GB堆( jvm失败时只使用1Gb )请帮助解决这个问题。
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 8192 bytes for committing reserved memory.
# Possible reasons:
# The system is o
我试图通过用不同的语言实现不同的算法来练习编程。关于插入排序的c++实现,我有两个问题。首先,为什么c++中的大多数实现都包含一个length参数,而其他实现(例如java )只访问for循环中的数组长度?下一个问题是,为什么大多数实现都在while循环中交换变量,而不是在最后交换变量呢?我已经包含了两个实现,以便更容易地讨论。
Java实现:
void insertionSort(int[] arr) {
int i, j, newValue;
for (i = 1; i < arr.length; i++) {
newValue = a
无法理解为什么选择排序作为java方法的实现不能像预期的那样工作。
class SelectionSort {
void SelectionSortMethod(int[] array) {
int min, temp = 0;
SwapClass swap = new SwapClass();
ArrayPrinting AP = new ArrayPrinting();
for (int i = 0; i < array.length; i++) {
min = i;
这是我想出的QuickSort随机化,但它经常抛出IndexOutOfBounds异常。我能帮上忙吗?谢谢!
import java.util.Random;
public class QuickSort {
void quickSort(int[] A, int start, int end) { // Initially: start = 0, end = n-1
while (start < end) {
int iOfPartition = randomisedPartition(A, start, end);
我在这个程序中遇到一个又一个错误。在这一点上,大部分事情似乎都是有序的,我可以正确地编译,但是当我运行程序时,当我被要求输入一个字母等级时,输入任何字母等级都会得到这样的结果:
Exception in thread "main" java.util.InputMisMatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextIn
我正在编写一个在两个2d行之间进行交换的java代码,我已经完成了它,但我想检查一下我的工作是否正确,以及它是否需要进行任何修改。 import java.util.*;
public class Main {
public static void printMatrix(int[][] matrix){
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++)
System.ou
这里是Java的初学者。我是不是可以说,将int i传递给n1,将int j传递给int n2?但是我的输出是在交换之后,i是1,j是2,为什么我的变量不能交换?编辑1:在看了其他帖子后,有人说没有原始数据的交换方法?那么,为什么我的讲师要创建这种交换方法,以避免混淆?
int i = 1;
int j = 2;
swap(i,j);
System.out.println("After swapping,i is " +i + ", j is " + j);
}
public static void swap(int n
我正在通过阅读" Java,如何编程“这本书来学习JAVA,我已经读到了第7章,其中谈到了数组和数组操作。在7.5节(案例研究:纸牌洗牌和交易模拟)中,它介绍了一个程序,该程序创建一副牌,对其进行洗牌,并显示洗牌后的牌,而不会有任何重复。
在程序中,它使用shuffle方法在创建卡片后对它们进行洗牌。该方法使用随机对象创建一个介于0和51之间的随机数,从牌组中选择一张牌并将其分配给一个数组。代码:
public void shuffle() {
// after shuffling, dealing should start at deck[ 0 ] again
这是一段很简单的代码。但结果仍然是错误的,我只是不明白为什么--有人吗?
使用数组int[] a = {3,1,0,2,5}并使用System.out.println(Arrays.toString(s.swap(a, a[0], a[1])));调用它,我得到3、2、0、1、5作为结果。这里发生了什么事?我期望1,3,0,2,5。-谢谢!
import java.util.Arrays;
public class MyClass {
public int[] swap(int[] array, int a, int b){
int temp =
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[5];
System.out.println("Enter the values in the array");
for (int i = 0; i < arr.length - 1; i++) {