简体   繁体   English

如何迭代比Java中列表大小指定的次数更多的次数

[英]how to iterate more number of times than size specified of list in java

I have a list of class type which holds value from object of class type in following way 我有一个类类型列表,它以以下方式保存类类型对象的值

public  List<Vacc_vs6> refarray_vac1(String fdate,String ldate) throws SQLException, ParseException {
    st_jsp.clear();
    try {  
        con = getConnection();
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
        String vs1 = "sql query";
        stmt.executeQuery(vs1);
        rs = stmt.getResultSet();

        while (rs.next()) {
            Vacc_vs6 ref = new Vacc_vs6();

            ref.setLogtime(rs.getString(1));
            ref.setBeam_current(rs.getString(2));
            ref.setBeam_energy(rs.getString(3));
            ref.setst4_vs6_bag1_rb(rs.getString(4));
            ref.setst4_vs6_bag2_rb(rs.getString(5));
            ref.setst4_vs6_bag3_rb(rs.getString(6));
            ref.setst4_vs6_bag4_rb(rs.getString(7));
            ref.setst4_vs6_bag5_rb(rs.getString(8));
            ref.setst4_vs6_bag6_rb(rs.getString(9));
            ref.setst4_vs6_bag7_rb(rs.getString(10));
            ref.setst4_vs6_bag8_rb(rs.getString(11));
            ref.setst4_vs6_bag9_rb(rs.getString(12));
            st_jsp.add(ref);                  
        }
    } catch (Exception e) {
        System.out.println("\nException in refarray_vac1 " + e);
    }          
    return st_jsp;
}

st_jsp is the list. st_jsp是列表。 Now I take another list of of class type and want to add values into it corresponding to every column of list st_jsp. 现在,我再获取一个类类型的列表,并想在其中添加与列表st_jsp的每一列相对应的值。 I also want to add only those values in the second list which satisfy the given if condition otherwise go to else and put null at particular columns. 我还想只在第二个列表中添加那些满足给定条件的值,否则转到else并在特定列中放置null。 For this the code is- 为此,代码是-

List<Vacc_vs6> new_list=new ArrayList<Vacc_vs6>();
double is = 9.5;
double js = 10.5;
int no=0;
for (no=0;no<st_jsp.size();no++) {
    String i= st_jsp.get(no).getBeam_current();
    double im = Double.parseDouble(i) ;
    if(im>is && im<js) {
        new_list.addAll(st_jsp);
    } else  {
        new_list.addAll(null);
    }
    is +=10;js +=10; 
}
  1. Problem-1--> how to add all the 12 columns corresponding to that index in new_list.addAll(???) ; 问题1->如何在new_list.addAll(???)添加与该索引对应的所有12列; Did I have to add column where I put ?? 我是否必须在我放置的位置添加列?

    ref.setLogtime(rs.getString(1)); ref.setLogtime(rs.getString(1)); ref.setBeam_current(rs.getString(2)); ref.setBeam_current(rs.getString(2));

  2. Problem-2 for(no=0;no<st_jsp.size();no++) loop will go till the size of st_jsp but if I do so then all values of st_jsp list will not be displayed because of else condition as I have to add 0 or null to those rows which do not satisfy the above if condition. 问题2 for(no=0;no<st_jsp.size();no++)循环将一直持续到st_jsp的大小,但是如果我这样做,那么由于其他条件, st_jsp list的所有值将不会显示,因为我必须向不满足上述if条件的行添加0null What to do for this? 为此该怎么做?

1 Problem-1--> **how to add all the 12 columns corresponding to that index in new_list.addAll(???);**Did I have to add column where I put ?? 1问题1-> **如何在new_list.addAll(???)中添加与该索引相对应的所有12列; **我必须在放置??的地方添加列吗?

To add only the object that satisfies your condition to the new list you should do: 要将满足条件的对象仅添加到新列表中,您应该执行以下操作:

if (im > is && im < js) {
    new_list.add(st_jsp.get(no));
}

The method addAll() adds all elements from one collection into another. 方法addAll()将一个集合中的所有元素添加到另一个集合中。 So, the way you are doing it, the first time your condition is satisfied you are adding every ref in st_jsp to new_list . 因此,按照您的操作方式,第一次满足您的条件时, st_jspnew_list每个引用添加到new_list And then you will keep re-adding the elements everytime new_list.addAll(st_jsp) is called. 然后,每次new_list.addAll(st_jsp)您将继续添加元素。 Using add() will add only the object you want. 使用add()将仅添加所需的对象。

2) Problem-2 for(no=0;no<st_jsp.size();no++) loop will go till the size of st_jsp but if I do so then all values of st_jsp list will not be displayed because of else condition as I ahve to add 0 or null to those rows whcih do not satisfy the above if condition.What to do for this 2)问题2 for(no=0;no<st_jsp.size();no++)循环将一直持续到st_jsp的大小,但是如果这样做,则由于其他条件, st_jsp list的所有值将不会显示,因为我如果不满足上述条件,则向这些行添加0null

You are doing the same action in else that you are doing when the if condition is satisfied, that is, adding all elements from st_jsp to new_list : if满足if条件,您将执行与else相同的操作, st_jsp new_list所有元素添加到new_list

if (im > is && im < js) {
    new_list.addAll(st_jsp);
} else {
    new_list.addAll(st_jsp);
}

This means that, after your loop, you will end with st_jsp.size() copies of st_jsp in new_list . 这意味着,你的循环之后,你会结束st_jsp.size()的副本st_jspnew_list

If you need to add an object containing null in all fields to new_list everytime an object does not satisfy your condition you first need to create this object, and then add it when the condition is not met. 如果每次对象不满足您的条件时都需要将所有字段中都包含null的对象添加到new_list ,则首先需要创建此对象,然后在不满足条件时将其添加。 Also, if you need to test for each element in st_jsp if it satisfies the conditions for all values of is and js , you need to place another loop inside the for loop. 另外,如果需要测试st_jsp每个元素是否满足isjs所有值的条件, is需要在for循环内放置另一个循环。

List<Vacc_vs6> new_list = new ArrayList<Vacc_vs6>();
for (int no=0; no < st_jsp.size(); no++) {
    double is = 9.5;
    double js = 10.5;
    double im = Double.parseDouble(st_jsp.get(no).getBeam_current());
    // flag to check if an element was added to new_list
    boolean added = false;
    while (is < 209.9 && js < 210.5) {
        if (im > is && im < js) {
            new_list.add(st_jsp.get(no));
            added = true;
            break;
        }
        is += 10;
        js += 10;
    }
    if (!added) {
        // If the element was not added, add an empty object,
        // assuming that no field is set when creating the object.
        new_list.add(new Vacc_vs6());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java如何分配比指定堆大小更多的内存 - How come Java can allocate more memory than the specified heap size 你如何让for循环迭代2.17b次以上? - How can you have a for loop iterate more than 2.17b times? 如何使用 Java 8 Stream 或 collections 框架计算列表中多个元素的出现次数 - How to count number of occurrences of more than one elements in a list using Java 8 Stream or collections framework 如何在 Java 中高效读取大小超过 100 MB 的文件 - How to read file of size more than 100 MB efficiently in Java 鉴于0 &lt; k &lt; n,并且在java中的O(k log n)时间,如何在大小为n的排序数组中获得超过n / k次的任何integer? - how to get any integer in a sorted array of size n that appear more than n/k times, given that 0 < k < n, and in O(k log n) time in java? 如果数字在列表中出现多次,如何生成错误 - How to generate an error if a number appears more than once in a list 如何检查一个数字是否包含多个相同的数字? (Java) - How to check if a number contains more than 1 of the same digit? (Java) 如何在Java中确保数字不超过2的数字 - How to make sure a number with digits no more than 2 in java 如何接受 Java 中任意多于两位的数字? - How to accept any number of more than two digits in Java? 如果调用scheduleWithFixedDelay方法的次数超过池大小,则ScheduledExecutorService的工作方式如何? - How works ScheduledExecutorService if call scheduleWithFixedDelay method more times than pool size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM