Escribir un programa que devuelve una nueva matriz que contiene todos los valores mayores que el primer valor de la matriz en java

faheem:

Escribir un programa que devuelve una nueva matriz que contiene todos los valores mayores que el primer valor de la matriz. Si no hay valores en la mayor variedad que el primer valor, devolver una matriz vacía. Si la matriz está vacía, devuelve una matriz vacía.

Resuelvo esta pregunta como esta:

public class RayGetFirst
{
    //method go will return an array
    //containing all values > the first value in the array
    //from the array parameter ray
    public static int[] go(int[] ray)
    {
        int first = ray[0];
    int[] result = new int[];
    for( int i = 1; i<ray.length; i++)
    {
      if(first < ray)
      {
        return result;
      }
    }
     return result;
  }
}

En primer lugar, separar el primer índice de la matriz y luego comenzó el índice para-bucle para 1 no de cero y después de comparar la primera matriz con el resto de la matriz. Pero yo no terminó con la respuesta correcta. ¿Alguien puede corregir esto con la misma lógica que estoy usando?

El corredor de este programa:

import java.util.*;
class Main 
{
  public static void main(String[] args)
  {
    RayGetFirst rt = new RayGetFirst();
    System.out.println( rt.go( new int[]{-99,1,2,3,4,5,6,7,8,9,10,5} ) );
        System.out.println( rt.go( new int[]{10,9,8,7,6,5,4,3,2,1,-99} ) );
        System.out.println( rt.go( new int[]{10,20,30,40,50,-11818,40,30,20,10} ) );
        System.out.println( rt.go( new int[]{32767} ) );
        System.out.println( rt.go( new int[]{255,255} ) );
        System.out.println( rt.go( new int[]{9,10,-88,100,-555,2} ) );
        System.out.println( rt.go( new int[]{10,10,10,11,456} ) );
        System.out.println( rt.go( new int[]{-111,1,2,3,9,11,20,1} ) );
        System.out.println( rt.go( new int[]{9,8,7,6,5,4,3,2,0,-2,6} ) );
        System.out.println( rt.go( new int[]{12,15,18,21,23,1000} ) );
        System.out.println( rt.go( new int[]{250,19,17,15,13,11,10,9,6,3,2,1,0} ) );    
        System.out.println( rt.go( new int[]{9,10,-8,10000,-5000,-3000} ) );
  }
} 

Las respuestas correctas con este corredor:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5]
[]
[20, 30, 40, 50, 40, 30, 20]
[]
[]
[10, 100]
[11, 456]
[1, 2, 3, 9, 11, 20, 1]
[]
[15, 18, 21, 23, 1000]
[]
[10, 10000]

Gracias

WJS:

Es necesario para devolver una matriz llena de valores> que el primero de la serie suministrado.

public class RayGetFirst {
    //method go will return an array
    //containing all values > the first value in the array
    //from the array parameter ray
    public static int[] go(int[] ray) {
        int first = ray[0];
        int k = 0;                           // set an index
        int[] result = new int[ray.length];  // allocate storage for values.
        for( int i = 1; i<ray.length; i++) {
           if(first < ray[i]) {
              result[k++] = ray[i];     // copy values
           }
        }

    // now establish a new array of length k
        int [] ret = new int[k];
    // and copy the first `k` values in `result` to that array.
    // Then return that array.
        for (int i = 0; i < k; i++) {
            ret[i] = result[i];
        }

        return ret;
    }
}

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=363941&siteId=1
Recomendado
Clasificación