KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > util > ArrayCaster


1 package JSci.util;
2
3 /**
4 * A simple convenience class for casting arrays.
5 * @author Daniel Lemire
6 */

7 public final class ArrayCaster extends Object JavaDoc {
8         private ArrayCaster() {}
9
10         public static float[] toFloat(double[] v) {
11                 final float[] ans=new float[v.length];
12                 for(int k=0;k<v.length;k++)
13                         ans[k]=(float) v[k];
14                 return(ans);
15         }
16         public static float[] toFloat(int[] v) {
17                 final float[] ans=new float[v.length];
18                 for(int k=0;k<v.length;k++)
19                         ans[k]=(float) v[k];
20                 return(ans);
21         }
22         public static float[][] toFloat(double[][] v) {
23                 final float[][] ans=new float[v.length][];
24                 for(int k=0;k<v.length;k++)
25                         ans[k]=toFloat(v[k]);
26                 return(ans);
27         }
28         public static float[][] toFloat(int[][] v) {
29                 final float[][] ans=new float[v.length][];
30                 for(int k=0;k<v.length;k++)
31                         ans[k]=toFloat(v[k]);
32                 return(ans);
33         }
34
35         public static double[] toDouble(float[] v) {
36                 final double[] ans=new double[v.length];
37                 for(int k=0;k<v.length;k++)
38                         ans[k]=(double) v[k];
39                 return(ans);
40         }
41         public static double[] toDouble(int[] v) {
42                 final double[] ans=new double[v.length];
43                 for(int k=0;k<v.length;k++)
44                         ans[k]=(double) v[k];
45                 return(ans);
46         }
47         public static double[][] toDouble(float[][] v) {
48                 final double[][] ans=new double[v.length][];
49                 for(int k=0;k<v.length;k++)
50                         ans[k]=toDouble(v[k]);
51                 return(ans);
52         }
53         public static double[][] toDouble(int[][] v) {
54                 final double[][] ans=new double[v.length][];
55                 for(int k=0;k<v.length;k++)
56                         ans[k]=toDouble(v[k]);
57                 return(ans);
58         }
59
60         public static int[] toInt(double[] v) {
61                 final int[] ans=new int[v.length];
62                 for(int k=0;k<v.length;k++)
63                         ans[k]=(int) v[k];
64                 return(ans);
65         }
66         public static int[] toInt(float[] v) {
67                 final int[] ans=new int[v.length];
68                 for(int k=0;k<v.length;k++)
69                         ans[k]=(int) v[k];
70                 return(ans);
71         }
72         public static int[][] toInt(float[][] v) {
73                 final int[][] ans=new int[v.length][];
74                 for(int k=0;k<v.length;k++)
75                         ans[k]=toInt(v[k]);
76                 return(ans);
77         }
78         public static int[][] toInt(double[][] v) {
79                 final int[][] ans=new int[v.length][];
80                 for(int k=0;k<v.length;k++)
81                         ans[k]=toInt(v[k]);
82                 return(ans);
83         }
84 }
85
86
Popular Tags