如何从二维数组列表中的特定位置提取整数?

发布于 2024-12-29 12:17:24 字数 230 浏览 1 评论 0原文

如果我有一个由 ArrayList 组成的 ArrayList,请说“biglist”。

[[1,2,3],[4,3,2],[5,1,2],[6,4,7],[7,1,2]]

我如何计算第一行中的所有 1(即 1 4 5 6 7,总共一个 1),第二行也是如此?

我在这方面失败了,所以任何帮助或指导将不胜感激。

If I have an ArrayList of ArrayLists say "biglist".

[[1,2,3],[4,3,2],[5,1,2],[6,4,7],[7,1,2]]

How could I tally all of the 1's in the first row (so 1 4 5 6 7, total of one 1), and the same for the second etc?

I lost on this so any help or guidance would be appreciated.

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

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

发布评论

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

评论(3

情域 2025-01-05 12:17:24
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
//...add your integer to the list

ArrayList<Integer> newList = new ArrayList<Integer>();
for(int i = 0; i < list.size(); i++)
{
    if(i == 2 || i == 3) //for instance if you want to exclude certain sublists in your list 
        continue;

    ArrayList<Integer> ints = list.get(i);
    if(ints.size() > 0)
        newList.add(ints.get(0 /* 0 or whatever part of inner list you want */));
}
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
//...add your integer to the list

ArrayList<Integer> newList = new ArrayList<Integer>();
for(int i = 0; i < list.size(); i++)
{
    if(i == 2 || i == 3) //for instance if you want to exclude certain sublists in your list 
        continue;

    ArrayList<Integer> ints = list.get(i);
    if(ints.size() > 0)
        newList.add(ints.get(0 /* 0 or whatever part of inner list you want */));
}
诗酒趁年少 2025-01-05 12:17:24

您是否尝试过类似的操作:

public ArrayList<ArrayList<Integer>> getElements(ArrayList<ArrayList<Integer>> bigList, int columnIndex){
    ArrayList<Integer> resultList = new ArrayList<Integer>();
    for ( ArrayList<Integer> al : bigList ){
        resultList.add(al.get(columnIndex));
    }
    return resultList;
}

注意:我说columnIndex是因为我将您的bigList视为矩阵。

Have you tried something like:

public ArrayList<ArrayList<Integer>> getElements(ArrayList<ArrayList<Integer>> bigList, int columnIndex){
    ArrayList<Integer> resultList = new ArrayList<Integer>();
    for ( ArrayList<Integer> al : bigList ){
        resultList.add(al.get(columnIndex));
    }
    return resultList;
}

NOTE: I say columnIndex because I see your bigList as a matrix.

甜味拾荒者 2025-01-05 12:17:24

我如何计算第一行中的所有 1(即 1 4 5 6 7,总共一个 1),第二行等也是如此?

您可以使用以下方法计算连续看到特定数字的次数:

int intWeAreLookingFor = 1;
int rowNumber=0;
for(ArrayList list: biglist){

    int numOfHits=0;
    rowNumber++;
    for(Integer i: list){

        if(i.equals(intWeAreLookingFor)){
            numOfHits++;
        }
    }
    System.out.printLine("The number '"+intWeAreLookingFor
        +"' was counted "+numOfHits+" times in row "+rowNumber+".");
}

对于示例数组 [[1,2,3],[4,3,2],[5,1,2] ,[6,4,7],[7,1,2]],这将打印出:

The number '1' was counted 1 times in row 1.
The number '1' was counted 0 times in row 2.
The number '1' was counted 1 times in row 3.
The number '1' was counted 0 times in row 4.
The number '1' was counted 1 times in row 5.

How could I tally all of the 1's in the first row (so 1 4 5 6 7, total of one 1), and the same for the second etc?

You can count the number of times you see a particular number in a row using something like:

int intWeAreLookingFor = 1;
int rowNumber=0;
for(ArrayList list: biglist){

    int numOfHits=0;
    rowNumber++;
    for(Integer i: list){

        if(i.equals(intWeAreLookingFor)){
            numOfHits++;
        }
    }
    System.out.printLine("The number '"+intWeAreLookingFor
        +"' was counted "+numOfHits+" times in row "+rowNumber+".");
}

For your sample array [[1,2,3],[4,3,2],[5,1,2],[6,4,7],[7,1,2]], this would print out:

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