java - 从不同数组转换为标准形式

标签 java android arrays string bitmap

我更喜欢一个小示例代码,我可以将其重新制作成更大范围的该问题,或者指向有关这些问题的特定问题/教程的链接。

我所做的是将位图数组存储到 ArrayList (因为我无法将其存储到 Bitmap[]),现在我想做的是 AL 并将其转换为 Bitmap[] 或从中获取内容并将其直接添加到我的变量位图 a1、a2、a3 中;//等等

我也希望这样做,但是对于 String[],我已经做了与上面相同的操作,除了将其存储在 String[] 中,现在我希望将其存储到变量 String b1, b2, b3;//等等

我也希望用 Double[] 来做到这一点,你知道该怎么做。 我已经四处寻找类似的东西,但我唯一接近找到相关的东西是字符串数组,但是代码/教程不是针对我的问题而制作的,我试图找出一种制作它的方法可能,但没有成功..

public class LVFinal extends Activity{


Bitmap a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, a51, a52, a53, a54, a55, a56, a57, a58, a59, a60, a61, a62, a63, a64, a65, a66, a67, a68, a69, a70, a71, a72, a73, a74, a75, a76, a77, a78, a79, a80, a81, a82, a83, a84, a85, a86, a87, a88, a89, a90, a91, a92, a93, a94, a95, a96, a97, a98, a99, a100;
CharSequence b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55, b56, b57, b58, b59, b60, b61, b62, b63, b64, b65, b66, b67, b68, b69, b70, b71, b72, b73, b74, b75, b76, b77, b78, b79, b80, b81, b82, b83, b84, b85, b86, b87, b88, b89, b90, b91, b92, b93, b94, b95, b96, b97, b98, b99, b100;
Double c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32, c33, c34, c35, c36, c37, c38, c39, c40, c41, c42, c43, c44, c45, c46, c47, c48, c49, c50, c51, c52, c53, c54, c55, c56, c57, c58, c59, c60, c61, c62, c63, c64, c65, c66, c67, c68, c69, c70, c71, c72, c73, c74, c75, c76, c77, c78, c79, c80, c81, c82, c83, c84, c85, c86, c87, c88, c89, c90, c91, c92, c93, c94, c95, c96, c97, c98, c99, c100;
ArrayList<Bitmap> aMaster;
Bitmap[] bitmapArray;
String[] sMaster;
Double[] dMaster;

private List<LVSecond> myList = new ArrayList<LVSecond>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    aMaster = ConnectMySQL.getRankingPic();
    sMaster = ConnectMySQL.getName();
    dMaster = ConnectMySQL.getRank();
    charSequenceRet(); //same as below pretty much
    bitmapReturn(); //this will return a1, a2, a3 and so on, but in normal bitmap(s) so i can use it in my code for custom listview
    doubleReturn(); // same as above pretty much

这是来自 ConnectMySQL 类

public static String[] getName(){
    connection();
    try {
        Connection connect = DriverManager.getConnection(host, username, password);
        PreparedStatement statement = connect.prepareStatement("SELECT users.displayname, users.gender FROM users, picture WHERE users.id=picture.user_id ORDER BY picture.finalsum DESC LIMIT 100");
        ResultSet rs = statement.executeQuery();
        ArrayList<String> list = new ArrayList<String>();
        rs.first(); 
        while (!rs.isAfterLast()){ 
            list.add(rs.getString("displayname"));
            rs.next(); 
        }

        result = new String[list.size()];
        result = list.toArray(result); 
        statement.close();
        connect.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /*for(int i =0; i<result.length; i++){
          System.out.println(result[i]);
    }*/
    return result;
}

public static void InsertImage(String user_id, String gender, Byte image, String filePath) {
    connection();
    try {
        Connection connect = DriverManager.getConnection(host, username, password);
        PreparedStatement statement = connect.prepareStatement("INSERT INTO picture (user_id, gender, image) VALUES(?, ?, ?)");
        statement.setString(2, user_id);
        statement.setString(3, gender);                     
        statement.setByte(8, image);
        statement.executeUpdate();
        statement.close();
        connect.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static Double[] getRank(){
    connection();
    try {
        Connection connect = DriverManager.getConnection(host, username, password);
        PreparedStatement statement = connect.prepareStatement("SELECT picture.finalsum FROM users, picture WHERE users.id=picture.user_id ORDER BY picture.finalsum DESC LIMIT 100");
        ResultSet rs = statement.executeQuery();
        ArrayList<Double> intList = new ArrayList<Double>();
        rs.first(); 
        while (!rs.isAfterLast()){ 
            intList.add(rs.getDouble("finalsum"));
            rs.next(); 
        }   

        rank = new Double[intList.size()];
        rank = intList.toArray(rank); 
        statement.close();
        connect.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /*for(int i =0; i<result.length; i++){
          System.out.println(result[i]);
    }*/
    return rank;
}


public static ArrayList<Bitmap> getRankingPic(){
    connection();
    ArrayList<Bitmap> bmpList = new ArrayList<Bitmap>();
    Bitmap bmp;
    byte[] byteArray;
    try {
        Connection connect = DriverManager.getConnection(host, username, password);
        PreparedStatement statement = connect.prepareStatement("SELECT picture.image FROM users, picture WHERE users.id=picture.user_id ORDER BY picture.finalsum DESC LIMIT 100");
        ResultSet rs = statement.executeQuery();
        rs.first(); 
        while (!rs.isAfterLast()){ 
            byteArray = null;
            bmp = null;
            while (byteArray == null){
            byteArray = rs.getBytes("image");
            }
            bmp = BitmapFactory.decodeByteArray(
            byteArray, 0,
            byteArray.length);
            bmpList.add(bmp);
            rs.next(); 
        }   
        statement.close();
        connect.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /*for(int i =0; i<result.length; i++){
          System.out.println(result[i]);
    }*/
    return bmpList;
}

编辑:问题: 1. 如何将 String[] 转换为普通字符串? String[]包含100个不同的字符串,现在我希望将这些字符串植入到b1、b2、b3等中

  1. 我有一个叫做 ArrayList 的东西,它与位图数组基本相同,但并不完全相同,所以我想知道如何将其转换为变量 a1、a2、a3 (这些是位图)变量)如果我不能这样做,我该如何将其转换为 Bitmap[] 然后呢?

  2. 如何将 Double[] 转换为普通 double ?存储为 c1、c2、c3 等

OBS:我已写入普通字符串,我希望将其转换为 CharSequence,我会尽快修复它!

最佳答案

我不完全确定你想要什么,但听起来你基本上想从 ArrayList 转换为数组。您可以使用 ArrayList 的 toArray 方法来做到这一点。

例如:

bitmapArray = new Bitmap[aMaster.size()];
bitmapArray = aMaster.toArray(bitmapArray);

更新的答案:

如何将 String[] 转为普通字符串?

首先,CharSequence是Java中的一个接口(interface),而不是一个类。您无法创建 CharSequence 对象。字符串是一个 CharSequence。查看http://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html如果您想了解更多内容。

现在,我没有看到为数组中的每个元素创建所有这些变量有什么好处。我想说这样做是一种不好的做法。为什么不直接使用 sMaster[x] 访问元素?

如何将 Double[] 转换为普通 double ?

从 Double 转换为 Double:

// assume before is a variable with some Double value
double after = (double) before;

将 Double[] 转换为 double[]:

double[] doubleArray = new double[dMaster.length];
for(int k = 0; k < doubleArray.length; k++(
{
    doubleArray[k] = (double) dMaster[k];
}

关于java - 从不同数组转换为标准形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25207802/

相关文章:

java - parseLong 方法在 Java 中如何工作?

java - 对仅使用原语编码的 Java 程序的引用

android - ImageView 的 OnTouchListener 中的问题

.net - 编译时数组边界

java - 在 Java 中生成 Antlr 解析器 : Not all inputs are read

java - 是否可以在@RequestParam中有条件地赋值Required?

java - Android 开发中的 Twitter JSON API

java - OpenCV - 二进制垫上的线条细化会导致随机水平虚线

javascript - Socketio 1.0 从所有客户端获取属性

c++ - "sizeof"在此帮助程序中如何确定数组大小?