小编Ali*_*Ali的帖子

如何使用递增的值来填充数组Ruby

我正在尝试解决http://projecteuler.net/problem=1.

我想创建一个方法,它接受一个整数,然后创建一个包含在它之前的所有整数的数组,并将整数本身作为数组中的值.

以下是我到目前为止的情况.代码不起作用.

def make_array(num)
  numbers = Array.new num  
  count = 1

  numbers.each do |number|
    numbers << number = count
    count = count + 1
  end

  return numbers    
end

make_array(10)
Run Code Online (Sandbox Code Playgroud)

ruby arrays project

5
推荐指数
1
解决办法
3395
查看次数

计数时间元素显示在链表Java中

我正在编写一个方法,允许我计算String类型的元素在String类型的LinkedList中出现的次数.我的代码如下所示不起作用.我继续在下面评论的行中获得索引超出界限.似乎无法找到这个bug

public int findDuplicate (LinkedList<String> e) {
    int j = 1;
    LinkedList<String> test = e;
    while (!test.isEmpty()){
        test = e;
        String value = test.pop();
        //Screws up here when i = 6 
        for(int i =0; i<=test.size() && test.get(i)!=null; i++){ 
            String value3 = test.get(i);
            if(e.get(i).equals(value) && i<=test.size()){
                String value2 = test.get(i); 
                j++;
                String Duplicate = e.get(i);
                e.remove(i);
            }
        }
        System.out.println(value + " is listed " + j + " times");

    }
    return j;
}
Run Code Online (Sandbox Code Playgroud)

使用hashmaps ..仍然无效public void findDuplicate(LinkedList e){

        Map<String,Integer> counts = …
Run Code Online (Sandbox Code Playgroud)

java loops

1
推荐指数
1
解决办法
601
查看次数

使用HashMaps Java

我正在编写一个方法,允许我计算String类型的元素在String类型的LinkedList中出现的次数.我的代码如下所示不起作用.我继续在下面评论的行中获得索引超出界限.似乎无法找到这个bug

public int findDuplicate (LinkedList<String> e) {
int j = 1;
LinkedList<String> test = e;
while (!test.isEmpty()){
    test = e;
    String value = test.pop();
    //Screws up here when i = 6 
    for(int i =0; i<=test.size() && test.get(i)!=null; i++){ 
        String value3 = test.get(i);
        if(e.get(i).equals(value) && i<=test.size()){
            String value2 = test.get(i); 
            j++;
            String Duplicate = e.get(i);
            e.remove(i);
        }
    }
    System.out.println(value + " is listed " + j + " times");

}
return j;
}
Run Code Online (Sandbox Code Playgroud)

使用hashmaps ..仍然无法正常工作

public void findDuplicate (LinkedList e) { …
Run Code Online (Sandbox Code Playgroud)

java hashmap

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×2

arrays ×1

hashmap ×1

loops ×1

project ×1

ruby ×1