Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中将数字数组四舍五入为3位_Java_Rounding_Out Of Memory - Fatal编程技术网

如何在java中将数字数组四舍五入为3位

如何在java中将数字数组四舍五入为3位,java,rounding,out-of-memory,Java,Rounding,Out Of Memory,我试图在java中将一个数字数组四舍五入到小数点后三位,原因是我遇到了OutOfMemoryError(数组超出了VM的限制)。我很好奇,是否有一种方法可以做到这一点,而不必写一个全新的方法或任何类似激烈的事情 编辑:这里是所有的代码 public class GuitarHero { public static void main(String[] args) { int index = 0; double sample = 0.0; String keyboard

我试图在java中将一个数字数组四舍五入到小数点后三位,原因是我遇到了OutOfMemoryError(数组超出了VM的限制)。我很好奇,是否有一种方法可以做到这一点,而不必写一个全新的方法或任何类似激烈的事情

编辑:这里是所有的代码

public class GuitarHero {
public static void main(String[] args) {


    int index = 0;
    double sample = 0.0;
    String keyboard ="1234567890qwertyuiopasdfghjklzxcvbnm,";

    GuitarString[] string = new GuitarString[keyboard.length()];

    for(int i = 0; i < 37; i++) {
        double concert = 110.0 * Math.pow(2,i-24);
        string[i] = new GuitarString(concert);
    }

    while (true){

        if (StdDraw.hasNextKeyTyped()) {
            char key = StdDraw.nextKeyTyped();
            index = keyboard.indexOf(key);

            if (index >= 0 && index < 37){
                string[index].pluck();
            }
            //sample = string[index].sample() + string[index+1].sample();
            //StdAudio.play(sample);
        }

        for(int i=0; i<37; i++){
            sample = string[i].sample();

            StdAudio.play(sample);
        }

            for(int i = 0; i < 37; i++){
                    string[i].tic();
            }           
        }   

    }
}
公共类吉他英雄{
公共静态void main(字符串[]args){
int指数=0;
双样本=0.0;
字符串键盘=“1234567890qwertyuiopasdfghjklzxcvnm,”;
GuitarString[]字符串=新的GuitarString[keyboard.length()];
对于(int i=0;i<37;i++){
双音乐会=110.0*数学功率(2,i-24);
弦[i]=新吉他弦(音乐会);
}
while(true){
if(StdDraw.hasNextKeyTyped()){
char key=StdDraw.nextKeyTyped();
索引=键盘。indexOf(键);
如果(索引>=0&&index<37){
字符串[index].pull();
}
//sample=string[index].sample()+string[index+1].sample();
//StdAudio.play(示例);
}

对于(inti=0;i在该循环的第一次迭代中,代码试图分配1681534603个双精度(44100/(110*2^-22))的数组,这将需要大约3GB的内存。我建议您找到一个不同的解决方案。

舍入?OOM错误?需要激烈的方法实现?舍入双精度不会导致使用更少的内存。伙计,您的问题显然不是来自可能的双精度舍入,而是来自您的GuitarString类中的内容(如果您的申请代码减少到您向我们展示的代码).你能展示导致OOM错误的代码吗?是的,一秒钟,但问题是总共涉及3个类,所以它有点多哈哈,哇,我没有意识到它占用了这么多内存,当我在家运行时,一切都很顺利,我没想到它占用了这么多内存,谢谢,伙计,我会想出另一个解决方案。我是怎么做到的顺便说一下,为了将来的参考,Java中的原语double是两个字节,千兆字节是1024*1024*1024字节。
public class GuitarString {

    private RingBuffer buffer; // ring buffer
    // YOUR OTHER INSTANCE VARIABLES HERE
    private int ticTimes = 0;
    // create a guitar string of the given frequency
    public GuitarString(double frequency) {
        // YOUR CODE HERE
    int N;
    N = (int)(44100/frequency);
    buffer = new RingBuffer(N);
    for (int i=1; i <=N; i++ ){
       buffer.enqueue(0.0);        
        }
    }

    // create a guitar string whose size and initial values are given by the array
    public GuitarString(double[] init) {
        // YOUR CODE HERE
    buffer = new RingBuffer(init.length);
    for (int i = 0; i < init.length; i++){
        buffer.enqueue(init[i]);
        }
    }

    // pluck the guitar string by setting the buffer to white noise
    public void pluck() {
        // YOUR CODE HERE
        while(!buffer.isEmpty()) buffer.dequeue();
    while(!buffer.isFull()){
    buffer.enqueue(Math.random()-0.5);
}
}

    // advance the simulation one time step
    public void tic() {
        // YOUR CODE HERE
    double value1, value2;
    value1 = buffer.dequeue();
    value2 = buffer.peek();
        buffer.enqueue(((value1+value2)/2)*0.996);
    ticTimes++;
    }

    // return the current sample
    public double sample() {
        // YOUR CODE HERE
    return buffer.peek();
    }

    // return number of times tic was called
    public int time() {
        // YOUR CODE HERE

    return ticTimes;
    }


  public static void main(String[] args) {
      int N = Integer.parseInt(args[0]);
      double[] samples = { .2, .4, .5, .3, -.2, .4, .3, .0, -.1, -.3 };  
      GuitarString testString = new GuitarString(samples);
      for (int i = 0; i < N; i++) {
          int t = testString.time();
          double sample = testString.sample();
          System.out.printf("%6d %8.4f\n", t, sample);
          testString.tic();
      }
  }

}
public class RingBuffer {
private int first;            // index of first item in buffer
private int last;             // index of last item in buffer
private int size;             // current number of items of buffer
private double[] buffer;

// create an empty buffer, with given max capacity
public RingBuffer(int capacity) {
    // YOUR CODE HERE
    buffer = new double[capacity];
    first =0;
    last  =capacity-1;
    size  =0;
}

// return number of items currently in the buffer
public int size() {
    // YOUR CODE HERE
    return size;
}

// is the buffer empty (size equals zero)?
public boolean isEmpty() {
    // YOUR CODE HERE
    if (size == 0) 
    return true;
    else 
    return false;
}

// is the buffer full (size equals array capacity)?
public boolean isFull() {
    // YOUR CODE HERE
    if (size == buffer.length)
    return true;  
    else
    return false;

}

// add item x to the end
public void enqueue(double x) {
    if (isFull()) { throw new RuntimeException("Ring buffer overflow"); }
    // YOUR CODE HERE
    last = (last+1)%buffer.length;
    buffer[last]=x;
    size++;
}

// delete and return item from the front
public double dequeue() {
    if (isEmpty()) { throw new RuntimeException("Ring buffer underflow"); }
    // YOUR CODE HERE
    double temp = buffer[first];
    first = (first+1)% buffer.length;
    size--;
    return temp;
}

// return (but do not delete) item from the front
public double peek() {
    if (isEmpty()) { throw new RuntimeException("Ring buffer underflow"); }
    // YOUR CODE HERE
    return buffer[first];
}

// a simple test of the constructor and methods in RingBuffer
public static void main(String[] args) {
    int N = Integer.parseInt(args[0]);
    RingBuffer buffer = new RingBuffer(N);
    for (int i = 1; i <= N; i++) {
    buffer.enqueue(i);
    }
    double t = buffer.dequeue();
    buffer.enqueue(t);
    System.out.println("Size after wrap-around is " + buffer.size);
    while (buffer.size() >= 2) {
    double x = buffer.dequeue();
    double y = buffer.dequeue();
    buffer.enqueue(x + y);
    }
    System.out.println(buffer.peek());
}

}