小编aer*_*lve的帖子

Scala中的模式匹配元组

试图在这里处理模式匹配 - 来自C++/Java背景,这对我来说非常陌生.

这个分支的要点是检查d元组列表的每个成员[格式为(字符串,对象).我想定义三个案例.

1)如果此函数中的计数器大于列表的大小(在另一个名为acc中定义),我想什么都不返回(因为没有匹配)2)如果key输入中的给定与列表中的元组匹配,我想返回它的值(或者,存储在元组中的任何内容.).3)如果没有匹配,则还有更多列表要迭代,递增并继续.

我的代码如下:

def get(key:String):Option[Any] = {

        var counter: Int = 0
        val flag: Boolean = false 

        x match {

                case (counter > acc) => None

                case ((d(counter)._1) == key) => d(counter)._2

                case _ =>   counter += 1
                }
Run Code Online (Sandbox Code Playgroud)

我的问题是,第一种情况似乎正确编译,第二种情况引发错误:

:36: error: ')' expected but '.' found.
                case ((d(counter)._1) == key) => d(counter)._2

第三个也是:

scala>         case _ =>  counter += 1
:1: error: illegal start of definition

但我认为这是因为第二个不正确.我的第一个想法是我没有正确地比较元组,但我似乎遵循索引到元组的语法,所以我很难过.任何人都可以引导我朝着正确的方向前进吗?

scala pattern-matching

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

Dijkstra的算法:为什么我最终会陷入这个例子?

我一直试图追踪Dijkstra的最短路径算法,用于以下无向图:


        (B)
       /   \
      /     \
  6  /       \ 9
    /         \ 
   /           \
  /             \
 (A)- 5 -(C)- 1 -(F)----2----(I)
   \            /
    \          / 
  4  \        / 2
      \      /
       \    /
        \  /
         (D)

For clarification:
(N) will represent nodes, numbers with no formatting will represent weights.
the edge between A and C has a weight of 5, 
the edge between C and F has a weight of 1. 

我将在此概述我的流程:

由于A是我的初始节点,因此算法从这里开始.由于D是更便宜的路径,因此算法遍历到D.现在将A标记为已访问,这意味着我们无法再次遍历它.

在D,我们很容易看到我们将搬到F.

F是我开始遇到麻烦的地方.由于最短的路径将引导我到C,我被困在两个访问过的节点之间,没有办法到达我.任何人都可以帮助我吗?

编辑:抱歉图表的家伙,这个问题最初是从手机上询问的.我会尽快解决这个问题.

theory graph

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

Java编译器不会进入我的for循环?

我正在尝试编写一个应用程序,它将使用随机数填充数组.在我尝试在populateArray方法中输入for循环之前,一切似乎都能正常工作.

import java.util.Random;

public class PerformanceTester {

//takes an empty array, but the size must be allocated BEFORE passing 
//to this function. Function takes a pre-allocated array and input size.
public static int[] populateArray(int[] inputArray, int n) {

    //Create the number generator
    Random generator = new Random();

    int length = inputArray.length;
    System.out.println("Inputted array is length: " + length);

    for (int i = 0; i == length; i++) {
        // for debugging purposes: System.out.println("For loop entered.");
        int random = generator.nextInt((2 * …
Run Code Online (Sandbox Code Playgroud)

java loops for-loop

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

C++ - 关于动态数组的范围

我有一个关于动态数组范围的快速问题,我认为这会导致我正在编写的程序中出现错误.此片段检查函数参数并分支到第一个或第二个,具体取决于用户传递的内容.

但是,当我运行程序时,我收到与范围相关的错误:

error: ‘Array’ was not declared in this scope

除非我对C++的了解失败,否则我知道在分支完成时,条件内创建的变量会超出范围.但是,我动态分配了这些数组,所以我无法理解为什么我不能在程序中稍后操作数组,因为指针应该保留.

        //Prepare to store integers
        if (flag == 1) {
                int *Array;
                Array = new int[input.length()]; 
            } 
        //Prepare to store chars
        else if (flag == 2) {
                char *Array;
                Array = new char[input.length()];
            }
Run Code Online (Sandbox Code Playgroud)

任何人都可以对此有所了解吗?

c++ pointers scope

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

标签 统计

c++ ×1

for-loop ×1

graph ×1

java ×1

loops ×1

pattern-matching ×1

pointers ×1

scala ×1

scope ×1

theory ×1