Java:如何遍历类型链表的数组并将其附加到数组

时间:2015-11-24 21:09:25

标签: java arrays algorithm linked-list radix

对于算法类中的家庭作业,我们必须编写一个实现Radix排序算法的程序。我最终以一种方式实现它,并且它正常运行。但是,我的代码中有一部分是for循环中看起来是否是块的残暴。我必须以正确的顺序从链接列表数组中检索项目,并将元素添加回Integer数组。我的一个同学和我花了很长时间试图弄清楚如何将这个块放入for循环但是却无法想出这样做。这就是我的问题,我如何将链表的对象放入一个不同的数组中。我为sort方法提出的代码如下:

private static Integer[] sort(Integer[] input, int place){
    //create an array of linked lists
    LinkedList<Integer>[] bucketsOut = new LinkedList[10];

    //initialize the linked lists
    for(int i=0; i < 10; i++){
        bucketsOut[i] = new LinkedList<Integer>();
    }

    int bucketPlacement = 0;
    //place every input into the correct bucket
    for(int i = 0; i < input.length; i++){
        bucketPlacement = getDigit(input[i].intValue(), place);
        bucketsOut[bucketPlacement].add(input[i]);
    }

    //Place the elements out of the linked lists into the correct place in input[]
    for(int i = 0; i < input.length; i++){ //for each input number
        if(bucketsOut[0].peekFirst() != null){  
            input[i] = bucketsOut[0].pollFirst().intValue();
        }else if(bucketsOut[1].peekFirst() != null){    
            input[i] = bucketsOut[1].pollFirst().intValue();
        }else if(bucketsOut[2].peekFirst() != null){    
            input[i] = bucketsOut[2].pollFirst().intValue();
        }else if(bucketsOut[3].peekFirst() != null){    
            input[i] = bucketsOut[3].pollFirst().intValue();
        }else if(bucketsOut[4].peekFirst() != null){    
            input[i] = bucketsOut[4].pollFirst().intValue();
        }else if(bucketsOut[5].peekFirst() != null){    
            input[i] = bucketsOut[5].pollFirst().intValue();
        }else if(bucketsOut[6].peekFirst() != null){    
            input[i] = bucketsOut[6].pollFirst().intValue();
        }else if(bucketsOut[7].peekFirst() != null){    
            input[i] = bucketsOut[7].pollFirst().intValue();
        }else if(bucketsOut[8].peekFirst() != null){    
            input[i] = bucketsOut[8].pollFirst().intValue();
        }else if(bucketsOut[9].peekFirst() != null){    
            input[i] = bucketsOut[9].pollFirst().intValue();
        }
    }
    //return sorted list for digit
    return input;
}

2 个答案:

答案 0 :(得分:1)

除了索引中的更改之外,当您进行一系列完全相同的操作时,这意味着您可以在<Grid> <GridView Margin="12,60" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollMode="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Auto"> <GridView.ItemsPanel> <ItemsPanelTemplate> <ItemsWrapGrid Orientation="Horizontal"/> </ItemsPanelTemplate> </GridView.ItemsPanel> <GridView.ItemContainerStyle> <Style TargetType="GridViewItem"> <Style.Setters> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Grid Background="#2A2A2A" Margin="5" Height="200" Width="300"> <ContentPresenter /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style.Setters> </Style> </GridView.ItemContainerStyle> <GridViewItem> <Grid> <TextBlock Text="SampleText" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" /> </Grid> </GridViewItem> <GridViewItem> <Grid> <TextBlock Text="SampleText" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" /> </Grid> </GridViewItem> <GridViewItem> <Grid> <TextBlock Text="SampleText" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" /> </Grid> </GridViewItem> <GridViewItem> <Grid> <TextBlock Text="SampleText" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" /> </Grid> </GridViewItem> <GridViewItem> <Grid> <TextBlock Text="SampleText" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" /> </Grid> </GridViewItem> <GridViewItem> <Grid> <TextBlock Text="SampleText" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" /> </Grid> </GridViewItem> <GridViewItem> <Grid> <TextBlock Text="SampleText" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" /> </Grid> </GridViewItem> <GridViewItem> <Grid> <TextBlock Text="SampleText" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="18" Foreground="White" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10" /> </Grid> </GridViewItem> </GridView> </Grid> 循环中执行此操作:

首次尝试

for

但是等等!这将继续通过所有桶。您原来的for (int j = 0; j < bucketsOut.length; j++ ) { // The part that is repeated again and again if (bucketsOut[j].peekFirst() != null) { input[i] = bucketsOut[j].pollFirst().intValue(); } } 结构实际上意味着一旦您点击右if,您就不会看到任何其他if

当条件成立时,这可以通过打破循环来完成:

改进版

else

或者你可以使用增强版 - 逻辑是相同的:

for (int j = 0; j < bucketsOut.length; j++ ) {
    if (bucketsOut[j].peekFirst() != null) {
        input[i] = bucketsOut[j].pollFirst().intValue();
        break; // Now the j loop will stop when we hit the first non-null.
    }
}

答案 1 :(得分:0)

感谢greybeard指出上面的else实际上导致第一个桶完全被清空。

考虑到这一点,将其变成循环并不难。请记住,您的基本想法是首先要复制第一个存储桶中的所有项目。这可以通过while循环轻松完成。

     while( bucketsOut[bucketIndex].peekFirst() != null &&
             inputIndex < input.length ) 
     {
        input[inputIndex++] = bucketsOut[bucketIndex].pollFirst();
     }

所以在这里,对于每个桶,我们复制,直到peek值为空。注意我在数组索引中使用post-increment。当您需要将一些元素复制到数组中时,这很常见。它允许我复制到增加的元素,而不必使用for循环提前修复确切的数字。

有了这个,其余的很容易。我刚刚在while循环中添加了一个for循环,以递增到下一个bucket。 while循环检查inputIndex,因此我们不会意外地尝试复制input的结尾。

  for( int inputIndex = 0, bucketIndex = 0; bucketIndex < bucketsOut.length;
          bucketIndex++ ) 
  {
     while( bucketsOut[bucketIndex].peekFirst() != null &&
             inputIndex < input.length ) 
     {
        input[inputIndex++] = bucketsOut[bucketIndex].pollFirst();
     }
  }

如果您想要花哨(或者如果您有更多元素),您可以在while循环后添加手动测试,这样如果input if( inputIndex >= input.length ) break; ,则不必测试更多存储桶满。

                                      List of installed extensions
Name    | Version |   Schema   |                             Description
------------+---------+------------+---------------------------------------------------------------------
 btree_gist | 1.0     | edrive     | support for indexing common datatypes in GiST
 plpgsql    | 1.0     | pg_catalog | PL/pgSQL procedural language
 postgis    | 2.1.7   | postgis    | PostGIS geometry, geography, and raster spatial types and functions
(3 rows)