Java 设计问题中的嵌套 RepeatingGrps

发布于 2024-11-09 16:50:49 字数 828 浏览 4 评论 0原文

编辑:我有两个对象,我必须将一个对象映射到另一个对象。问题是属性不完全匹配。我映射的对象是扁平的,有一堆 getter 和 setter。一切都非常好并且易​​于使用。 我必须将其映射到一个具有不同结构的对象,该对象位于诸如 SecAltIDGrp[] 之类的组和如下所示的属性管中。这是使用

ObjectToMapTo.setSecAltIDGrp(SecAltIDGrp[]) 

我有一些重复组来设置的,我必须将它们映射到平面对象的 getter 值,问题是这样的。要映射到的对象具有 Grps 的 Array[],必须由平面对象中的各种不同的 getter 填充。我想不出一个干净的方法来做到这一点。

例如,我有一个代码,具有以下结构的注释数组。

    public SecAltIDGrp[] populateComments(int NoComments)
    {

        SecAltIDGrp[] x = new SecAltIDGrp[NoComments];
        for(int i; i < NoComments; i++)
        {
            x[i].setAltID(obj.getVal);
            x[i].setAltIDSource(arg0, arg1);
        }
    }

然而数组的每个元素都由来自平面对象的不同 getter 填充...

我无法很好地执行此操作,在数组中插入 if 语句并将对象作为参数传递。这是相当可怕的编码。

我应该为许多这样的团体做这件事。一些数组元素设置器需要数组本身。

EDIT: I have two Objects I must map one to the other. The problem is that the attributes don't exactly match up. The object coming in I am mapping from is flat has a bunch of getters and setters. All very nice and easy to work with.
I have to map this to a an Object which has somewhat of a different structure, within Groups such as SecAltIDGrp[] and attritubes such as those below. Which is set using

ObjectToMapTo.setSecAltIDGrp(SecAltIDGrp[]) 

I have a few repeating groups which I must map with values from a getter from the flat object the problem is this. The object to be mapped to has Array[]s of Grps which must be populated by various different getters from the flat object. I cannot think of a clean way to do this.

For instance, I have a code an array of comments which has the following structure.

    public SecAltIDGrp[] populateComments(int NoComments)
    {

        SecAltIDGrp[] x = new SecAltIDGrp[NoComments];
        for(int i; i < NoComments; i++)
        {
            x[i].setAltID(obj.getVal);
            x[i].setAltIDSource(arg0, arg1);
        }
    }

however each element of the array the is populated by a different getter from the flat object...

I cannot thing a nice of doing this bar inserting a if statement within the array and passing in the object as a parameter. This is rather horrific coding.

I am supposed to do this for a number of such groups. With some of the array element setters requiring arrays themselves.

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

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

发布评论

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

评论(1

梦太阳 2024-11-16 16:50:50

尝试使用 HashMap,而不是使用 Array,这将帮助您更轻松地传递/存储对象。
如果您对 hashmap 键并不真正感兴趣,那么您可以简单地使用 ArrayList。

HashMap 将存储需要的组以及键。这里的键类似于您计划使用的数组索引。

public HashMap< Integer , SecAltIDGrp > populateComments( int NoComments )
{
    HashMap< Integer , SecAltIDGrp > hmSec = new HashMap< Integer , SecAltIDGrp >();
    for(int i; i < NoComments; i++)
    {
          SecAltIDGrp x = new SecAltIDGrp();

          x.setAltID(obj.getVal);
          x.setAltIDSource(arg0, arg1);

          hmSec.put( i , x );
    }

    return hmSec;
}

public class SecAltIDGrp{
   String altId = "";
   String altIdSource = "";

   public void setAltIDSource(  String altIDSource )
   {
       this.altIdSource = altIDSource;
   }

   public void setAltID( String altId )
   {
        this.altId = altID;
   }
}

确保 getter 和 setter 也将具有适当的参数来满足泛型,以防止在编译期间出现任何警告。

public class ObjectToMapTo
{

   private HashMap< Integer , SecAltIDGrp > hmPrivGrp = new HashMap< Integer,SecAltIDGrp >();

   public void setSecAltIDGrp(  HashMap< Integer , SecAltIDGrp > hmSecAltIDGrp)
   {
          this.hmPrivGrp = hmSecAltIDGrp;
   }
}

这就是您设置特定组的方式。存储哈希图而不是数组。
ObjectToMapTo.setSecAltIDGrp( hmSec );

您将能够迭代哈希映射并能够检索单个 SecAltIDGrp

一些需要阅读的内容:
http://www.javadeveloper.co.in/java-example /java-hashmap-example.html

Instead of using Array, try using HashMaps, which will help you pass/store the objects more easily.
If you are not really interested with the hashmap keys, then you can simply use ArrayList.

The HashMap will store the require group, along with the key. The key here will be similar to the array Index, you were planning to use.

public HashMap< Integer , SecAltIDGrp > populateComments( int NoComments )
{
    HashMap< Integer , SecAltIDGrp > hmSec = new HashMap< Integer , SecAltIDGrp >();
    for(int i; i < NoComments; i++)
    {
          SecAltIDGrp x = new SecAltIDGrp();

          x.setAltID(obj.getVal);
          x.setAltIDSource(arg0, arg1);

          hmSec.put( i , x );
    }

    return hmSec;
}

public class SecAltIDGrp{
   String altId = "";
   String altIdSource = "";

   public void setAltIDSource(  String altIDSource )
   {
       this.altIdSource = altIDSource;
   }

   public void setAltID( String altId )
   {
        this.altId = altID;
   }
}

Make sure the getters and setters will also have the appropriate parameters to satisfy the generics to prevent any warnings during compilation.

public class ObjectToMapTo
{

   private HashMap< Integer , SecAltIDGrp > hmPrivGrp = new HashMap< Integer,SecAltIDGrp >();

   public void setSecAltIDGrp(  HashMap< Integer , SecAltIDGrp > hmSecAltIDGrp)
   {
          this.hmPrivGrp = hmSecAltIDGrp;
   }
}

this is how you set the particular group. store the hashmap instead of an array.
ObjectToMapTo.setSecAltIDGrp( hmSec );

You will be able to iterate through the hashmap and be able to retrieve individual SecAltIDGrp

Some stuff to read up:
http://www.javadeveloper.co.in/java-example/java-hashmap-example.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文