f#中的Array2D和锯齿状数组

时间:2014-03-22 10:23:03

标签: arrays f# jagged-arrays

我写了这个f#代码来创建一个锯齿状(非矩形)数组。

open System

type MatrizIrregular() = 
  let irregular = Array2D.zeroCreate 5

  member This.Inicializar() =
    for f in 0 .. 4 do
      printf "Introduzcal el número de columnas de la matriz de la fila %d: " f
      let res = Console.ReadLine()
      let col = Int32.Parse(res)
      Array2D.set irregular f (col)
      for c in 0 .. col - 1 do
        printf "Introduzca el valor de la columna %d: " c
        let res2 = Console.ReadLine()
        Array2D.set irregular f c (Int32.Parse(res2))

  member This.Mostrar() = 
    for f in 0 .. 4 do
    for f in 0 .. irregular.[f,c].Lenght - 1 do
      printf "%d " irregular.[f,c]
      printf "\n" 

let matriz = MatrizIrregular()
matriz.Inicializar()
matriz.Mostrar()

我在第11,15行

上遇到错误
  

此表达式应该具有类型' a [,]但这里的类型为int - > ' B [,]

和第19,20行

  

运营商' expr。[idx]'已基于此程序点之前的信息用于不确定类型的对象。考虑添加更多类型约束。

我想在f#

中做这样的java代码
import java.util.Scanner;
public class MatrizIrregular {
    public Scanner teclado = new Scanner(System.in);
public int[][] irregular;

public void inicializar(){
    irregular = new int[5][];
    for(int f=0;f<irregular.length;f++){
            System.out.print("Introduzca el numero de columnas del la fila de la matriz "+f+": ");
    int col = teclado.nextInt();
    irregular[f] = new int[col];
    for(int c =0; c<irregular[f].length;c++){
        System.out.print("Introduzca el elemento de la columna "+c+": ");
            irregular[f][c] = teclado.nextInt();
            }
        }
    }

public void mostrar(){
    for(int f=0;f<irregular.length;f++){
            for(int c=0;c<irregular[f].length;c++){
               System.out.print(irregular[f][c]+" ");
            }
            System.out.print("\n");
        }
}

public static void main(String[] ar){
     MatrizIrregular matriz = new MatrizIrregular();
     matriz.inicializar();
     matriz.mostrar();
}

}

2 个答案:

答案 0 :(得分:2)

你不能像这样做出锯齿状的2D数组。 Array2D和类型[,],只能用于矩形2D数组。

要了解如何使用锯齿状数组,请查看this link


我想对您对Array2D.zeroCreate的使用发表评论。它有这种类型:

val it : (int -> int -> 'a [,])

当你给它两个整数时,它会给你一个数组。如果只给它一个整数:

let irregular = Array2D.zeroCreate 5
val irregular : (int -> '_a [,])     (* Also complains about generic types. *)

你得到一个函数,当给定缺少的第二个维度时,它会给你一个数组。您没有获得具有未指定的第二维的数组。

(也许可以尝试在解释器中使用let f x y = x + yf 1;请查看类型是什么,并尝试将f应用于一两件事。)

答案 1 :(得分:2)

2D数组不是不规则的,因此与java类似,您需要声明array arrayint的{​​{1}}。在F#中,int array array和zeroCreate需要知道它要为什么类型初始化数组。还要注意缩进,你的嵌套for循环没有正确缩进。

open System

type MatrizIrregular() = 
  let irregular : int array array = Array.zeroCreate 5

  member This.Inicializar() =
    for f in 0 .. 4 do
      printf "Introduzcal el número de columnas de la matriz de la fila %d: " f
      let res = Console.ReadLine()
      let col = Int32.Parse(res)
      let cols : int array = Array.zeroCreate col
      for c in 0 .. col - 1 do
        printf "Introduzca el valor de la columna %d: " c
        let res2 = Console.ReadLine()
        Array.set cols c (Int32.Parse(res2))
      Array.set irregular f cols

  member This.Mostrar() = 
    for f in 0 .. 4 do
      for c in 0 .. (Array.length irregular.[f]) - 1 do
        printf "%d " irregular.[f].[c]
      printf "\n" 

let matriz = MatrizIrregular()
matriz.Inicializar()
matriz.Mostrar()

不是最有效的答案,但它是被要求的。

相关问题