【问题标题】:Converting from different arrays to standard form从不同的数组转换为标准形式
【发布时间】:2014-08-08 16:15:12
【问题描述】:

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

我所做的是将一组位图存储到 ArrayList(因为我无法将其存储到 Bitmap[]),现在我想要做的是 AL 并将其变成 Bitmap[] 或从中获取内容并将其直接添加到我的变量 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[] 变成普通的双打?存储为 c1、c2、c3 等

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

【问题讨论】:

  • 请贴一些代码。真的不清楚你有什么,你想要什么。
  • 您无法使用 .toArray() 转换为字符串数组并从那里开始工作?
  • @KarolS 我已经编辑过了,应该更清楚,如果没有请询问。
  • 你能用一个问题编辑吗(...?)它会迫使你的问题的症结暴露出来。我对你想要什么有点开放。谢谢。
  • 好的,我知道你想要什么。对不起

标签: java android arrays string bitmap


【解决方案1】:

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

例如:

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

更新答案:

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

首先,CharSequence 在 Java 中是一个接口,而不是一个类。您不能创建 CharSequence 对象。一个字符串是一个 CharSequence。如果您想了解更多信息,请查看http://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html

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

如何将 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];
}

【讨论】:

  • 谢谢,但与堆栈上的普通问题不同,我又问了几个.. 我知道这就是为什么你不应该这样做,但它们有很多共同点。这将有很大帮助,谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-09-02
  • 2016-01-25
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 2016-08-23
  • 2020-11-15
  • 2021-03-13
相关资源
最近更新 更多