将 Hashtable 数组转换为对象数组

发布于 2024-09-11 22:30:57 字数 1695 浏览 2 评论 0原文

我的问题是我无法将哈希表数组转换为对象数组。这里的内容读取读取 xls 文件中的每一行,并将其保存在哈希表数组中,格式如下:

roleName=testRole981, admin=admin, projectName=Automateme, userPassword=aspiresys12, roleDesc=grant[testplan_execute],grant [testplan_create_build],grant[testplan_metrics], adminPass=admin, userName=mur

我想将该 hashTable 数组转换为对象数组,以便我可以将 testNG 测试用例中的那些数组作为 DataProvider 传递。

public class DriverScript {
 //public boolean isExecuted = true;
 Object[][] Obj = new Object[100][100];
 @SuppressWarnings("unchecked")
  @DataProvider(name="Test")
  public Object[][] ValidDataProvider() {
  Utilities util = new Utilities();
  String pathValue = Utilities.LocatingXls("Data.xls");
  Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1"); 
   Object[][] Obj = new Object[100][100];
   for(int i=0;i<hashDrv.length;i++)
   {
    System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
    Obj[i][0] = hashDrv[i];
   }
  System.out.println("outsideloop" + Obj[0][0]); 
  return  Obj;
  }

 @SuppressWarnings("unchecked")
 @Test(dataProvider = "Test")
    public  void methodtest(Hashtable <String, String> a)

 {
/* Utilities util = new Utilities();
 String pathValue = Utilities.LocatingXls("Data.xls");
 Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1"); 
 for(int i=0;i<hashDrv.length;i++)
 {
  System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
  Scenario1 scnTst=new Scenario1(hashDrv[i]);
  scnTst.check1();
 }
  if(!isExecuted)
  {
   Assert.fail("falied");
  }
 }*/

}}

My problem here is i cant able to convert the Hashtable array into object array. here content reading reads each and every row from the xls file and saves it in the hashtable array in this format:

roleName=testRole981, admin=admin, projectName=Automateme, userPassword=aspiresys12, roleDesc=grant[testplan_execute],grant[testplan_create_build],grant[testplan_metrics], adminPass=admin, userName=mur.

I want to convert that hashTable array into an object array so that i can pass those in my testNG test cases as a DataProvider.

public class DriverScript {
 //public boolean isExecuted = true;
 Object[][] Obj = new Object[100][100];
 @SuppressWarnings("unchecked")
  @DataProvider(name="Test")
  public Object[][] ValidDataProvider() {
  Utilities util = new Utilities();
  String pathValue = Utilities.LocatingXls("Data.xls");
  Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1"); 
   Object[][] Obj = new Object[100][100];
   for(int i=0;i<hashDrv.length;i++)
   {
    System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
    Obj[i][0] = hashDrv[i];
   }
  System.out.println("outsideloop" + Obj[0][0]); 
  return  Obj;
  }

 @SuppressWarnings("unchecked")
 @Test(dataProvider = "Test")
    public  void methodtest(Hashtable <String, String> a)

 {
/* Utilities util = new Utilities();
 String pathValue = Utilities.LocatingXls("Data.xls");
 Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1"); 
 for(int i=0;i<hashDrv.length;i++)
 {
  System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
  Scenario1 scnTst=new Scenario1(hashDrv[i]);
  scnTst.check1();
 }
  if(!isExecuted)
  {
   Assert.fail("falied");
  }
 }*/

}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2024-09-18 22:30:57

由于使用此数据提供程序的测试方法有一个参数,因此 Object[][] 数组的第二个维度应该是 1,而不是 100。也许类似于以下(未经测试)的代码:

@SuppressWarnings("unchecked")
@DataProvider(name="Test")
public Object[][] validDataProvider() {
    Utilities util = new Utilities();
    String pathValue = Utilities.LocatingXls("Data.xls");
    Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[])
        util.contentReading(pathValue, "Scenario1"); 
    Object[][] obj = new Object[hashDrv.length][1];
    for(int i=0; i<hashDrv.length; i++) {
        System.out.println("cont vector reading" 
            + hashDrv[i].get("projectName"));
        obj[i][0] = hashDrv[i];
    }
    System.out.println("outsideloop" + obj[0][0]); 
    return obj;
}

Since the test method using this data provider has one parameter, the second dimension of the Object[][] array should be 1, not 100. Perhaps something like this (untested) code:

@SuppressWarnings("unchecked")
@DataProvider(name="Test")
public Object[][] validDataProvider() {
    Utilities util = new Utilities();
    String pathValue = Utilities.LocatingXls("Data.xls");
    Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[])
        util.contentReading(pathValue, "Scenario1"); 
    Object[][] obj = new Object[hashDrv.length][1];
    for(int i=0; i<hashDrv.length; i++) {
        System.out.println("cont vector reading" 
            + hashDrv[i].get("projectName"));
        obj[i][0] = hashDrv[i];
    }
    System.out.println("outsideloop" + obj[0][0]); 
    return obj;
}
夜清冷一曲。 2024-09-18 22:30:57

Hashtable 类实现 Map 接口。该接口有一个名为 values() 的方法 - 返回一个 Collection。然后,您可以对该集合调用 toArray() 来获取数组。

示例:

Hashtable h = // .... initialised and populated somewhere else
Collection c = h.values();
Object[] objectArray = c.toArray();

问题:

  1. 上面的代码没有使用泛型,因此到处都会出现警告。
  2. Hashtable 可能不是最适合您使用的类,请尝试 HashMapHashTable 是同步的,因此如果您不希望线程安全,那么使用该类将导致代码变慢。
  3. 您确实应该对接口进行编程:Map map = new HashMap();(如果您愿意,也可以使用Hashtable

The Hashtable class implements the Map interface. This interface has a method on it called values() - that returns a Collection. You can then call toArray() on that collection to get an array.

Example:

Hashtable h = // .... initialised and populated somewhere else
Collection c = h.values();
Object[] objectArray = c.toArray();

Issues:

  1. The code above doesn't use generics, so there will be warnings everywhere.
  2. Hashtable is probably not the best class for you to use, try HashMap. HashTable is synchronized, so if you're not looking to be thread safe, using that class will result in slower code.
  3. You should really be programming to an interface: Map map = new HashMap(); (or Hashtable if you prefer.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文