GVKun编程网logo

Java 中的 split() 方法不适用于点 (.)(java中split方法的作用)

10

在本文中,我们将详细介绍Java中的split()方法不适用于点(.)的各个方面,并为您提供关于java中split方法的作用的相关解答,同时,我们也将为您带来关于'contains'方法不适用于Ar

在本文中,我们将详细介绍Java 中的 split() 方法不适用于点 (.)的各个方面,并为您提供关于java中split方法的作用的相关解答,同时,我们也将为您带来关于'contains' 方法不适用于 ArrayList,还有其他方法吗?、.style.display="none" 不适用于点击功能、c# – 扩展方法不适用于子类?、c# – 扩展方法不适用于接口的有用知识。

本文目录一览:

Java 中的 split() 方法不适用于点 (.)(java中split方法的作用)

Java 中的 split() 方法不适用于点 (.)(java中split方法的作用)

我准备了一个简单的代码片段,以便将错误部分与我的 Web 应用程序分开。

public class Main {    public static void main(String[] args) throws IOException {        System.out.print("\nEnter a string:->");        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String temp = br.readLine();        String words[] = temp.split(".");        for (int i = 0; i < words.length; i++) {            System.out.println(words[i] + "\n");        }    }}

我在构建 Web 应用程序 JSF 时对其进行了测试。我只想知道为什么上面的代码temp.split(".")不起作用。该声明,

System.out.println(words[i]+"\n");

在控制台上不显示任何内容意味着它不会通过循环。当我将temp.split()方法的参数更改为其他字符时,它像往常一样工作得很好。可能是什么问题?

答案1

小编典典

java.lang.String.split在正则表达式上拆分,在正.则表达式中表示“任何字符”。

试试temp.split("\\.")

'contains' 方法不适用于 ArrayList<int[]>,还有其他方法吗?

'contains' 方法不适用于 ArrayList,还有其他方法吗?

如何解决''contains'' 方法不适用于 ArrayList<int[]>,还有其他方法吗??

如果 ArrayList 还没有 int[],我想向它添加一个 int[],但由于某种原因,它不起作用。在这种情况下,arrlist 是 ArrayList<int[]>,arr 是 int[]。此代码位于 for 循环中,其中 arr 在循环中定义,因此 arr 中的值会发生变化。即使我打印出 arrlist 并且它有 arr,代码总是会说 arrlist 不包含 arr。是否有另一种方法来检查 ArrayList 是否包含 int[]

int n = scan.nextInt();
ArrayList<int[]> arrlist = new ArrayList<>();
int[][] coordinates = new int[n][2];
boolean[] isTrue = new boolean[n];
for (int j = 0; j < n; j++) {
    int[] arr = new int[2];
    arr[0] = coordinates[j][0];
    arr[1] = coordinates[j][1];
    if (arrlist.contains(arr)) {
        isTrue[j] = true;
    } else {
        arrlist.add(arr);
    }
}

解决方法

考虑这个代码:

int[] a = {1,2};
int[] b = {1,2};
System.out.println(a.equals(b));

你明白为什么输出是“false”吗?

这是导致您出现问题的原因:根据 ''equals'' 方法,内容相同的两个数组不相等,该方法在此处显式调用,并在您的 List<> 示例中隐式调用。

只要您想使用 int[] 数组,我认为没有一个简单的解决方法。您不能定义 equals 方法。我认为,您最好的方法是根本不使用数组来包含坐标。

定义一个类,其实例包含所需的两个整数:

class Point {
    int x,y;

    Point(int x,int y) {
        this.x = x;
        this.y = y;
    }

    public boolean equals(Object o) {
        return o instanceof Point
                && x == ((Point) o).x
                && y == ((Point) o).y;
    }
}

然后保留一个 ArrayList<Point>。 “包含”现在将按预期工作。

(您还应该在 Point 类中定义 hashCode,但我跳过了它,它与答案并不直接相关)。

,

由于数组不是基于每个元素进行比较,因此您不能使用由 ArrayList 实现的包含。您可以使用 Arrays 类执行以下操作。创建一个使用 List 和要检查的数组的辅助方法。请记住,数组不像集合,所以相等取决于顺序。

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    List<int[]> arrlist = new ArrayList<>();
    int[][] coordinates = new int[n][2];
    boolean[] isTrue = new boolean[n];
    for(int j = 0; j < n; j++) {
        int[] arr = new int[2];
        arr[0] = coordinates[j][0];
        arr[1] = coordinates[j][1];
        if (contains(arrlist,arr)) {
            isTrue[j] = true;
        } else {
            arrlist.add(arr);
        }
    }
}
private static boolean contains(List<int[]> list,int[] b) {
    for (int[] a : list) {
        if (Arrays.equals(a,b)) {
            return true;
        }
    }
    return false;
}

您总是可以只使用 List<List<Integer>> 而不是 List<int[]>,因为大家都知道数组不能很好地与列表配合使用。

,

在这种情况下,在创建 ArrayList 实例时,您可以使用自定义方法覆盖它的 contains 方法,比较内容两个数组,而不是比较两个数组对象的引用。此代码应该适用于 SRANDOM

Java 7

对于比较两个数组的内容的比较器,您可以使用 TreeSet 代替 ArrayList。然后你可以使用 TreeSet.contains 方法如下:

int[] a = {1,2};

ArrayList<int[]> arrayList = new ArrayList<>() {
    @Override
    public boolean contains(Object that) {
        for (int i = 0; i < this.size(); i++)
            if (Arrays.equals(this.get(i),(int[]) that))
                return true;
        return false;
    }
};

arrayList.add(a);
System.out.println(arrayList.size());      // 1
System.out.println(arrayList.contains(a)); // true
System.out.println(arrayList.contains(b)); // true
TreeSet<int[]> treeSet = new TreeSet<>(Arrays::compare);

treeSet.add(new int[]{1,2});
treeSet.add(new int[]{1,3});

System.out.println(treeSet.size());                    // 2
System.out.println(treeSet.contains(new int[]{1,2})); // true
System.out.println(treeSet.contains(new int[]{1,3})); // true

另见:
• Check if an array exists in a HashSet<int[]>
• How to make two arrays having different references?

,

您需要将 arr 中的每个 int 值与您的 ArrayList arrlist 中包含的所有数组的相应 int 值进行比较。
最短的解决方案应该是以下 lambda:

boolean contains = arrlist.stream().anyMatch( a -> arr[0] == a[0] && arr[1] == a[1] );

anyMatch 找到第一个匹配项后立即停止

,

试试这个:

if(arrlist.indexOf(arr)!=-1){
   isTrue[j] = true;
}

我认为在检查数组对象是否在列表中时,.contains 方法可能会发生一些奇怪的事情。

.style.display=

.style.display="none" 不适用于点击功能

您正在混合使用 jQuery 和 Javascript API,但您可以分别解决这个问题

$(".btn-start").click(function(){
    $(".winning-message").each(function() {
      this.style.display="none";
    });
});

另一种方法是使用 jQuery 的 .css 函数

    $(".winning-message").css("display","none");
,

您可以直接使用 .hide()

$(".winning-message").hide();

现场示例:

$(document).ready(function() {
  $(".btn-start").click(function() {
    $(".winning-message").hide();
  });
});
.winning-message {
  display: grid;
  position: absolute;
  width: $width-board;
  height: $height-board;
  justify-content: center;
  align-content: center;
  justify-items: center;
  align-items: center;
  background-color: rgba(22,22,0.336);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="submit">Start</button>
<divid="winningMessage">
  <div data-winning-message-text>
    <h1>Player 1 Won!</h1>
  </div>
  <button onclick="startGame()">RESTART</button>
</div>

,

您已向尚未在 restartButton 上定义的函数添加了内联点击引用。你正在混合 JQuery 和 Javascript 作为 ControlAltDel 提到。

以下示例已完全注释,您的案例中的关键行是:$(".winning-message").hide();

// Run startGame function on startup
// Uncomment the line below to automatically hide the message / start the game on load
//startGame()

// Add function to start button
$(".btn-start").click(function() {

  startGame();

});

// Define startGame function
function startGame() {

  // Hide winning message
  $(".winning-message").hide();
  // The line below acts in a similar way
  //$(".winning-message").css('display','none');

}

// Add click event to winButton
$(".winButton").click(function() {

  // Show winning message
  $(".winning-message").show();
  // The line below acts in a similar way
  //$(".winning-message").css('display','inherit')

});
.winning-message {
  display: grid;
  position: absolute;
  width: $width-board;
  height: $height-board;
  justify-content: center;
  align-content: center;
  justify-items: center;
  align-items: center;
  background-color: rgba(22,0.336);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button type="submit">Start</button>

<button>Show winning message</button>

<divid="winningMessage">
  <div data-winning-message-text>
    <h1>Player 1 Won!</h1>
  </div>
  <button onclick="startGame()">RESTART</button>

</div>

,

这里有两个问题。

首先是您试图在 jQuery 对象上使用原生 JS 属性。这是行不通的。当您实例化了一个 jQuery 对象时,请使用 jQuery 方法来隐藏该元素。 hide() 在这种情况下:

jQuery($ => {
  $(".btn-start").click(function() {
    $(".winning-message").hide();
  });
});

第二个问题是在您的 HTML 中。在 Codepen 中,您的完整 HTML 显示 .btn-start 元素是一个 <button type="submit">。因此,单击此按钮将提交父级 form 并将用户重定向到该 action 元素的 form 中提供的位置。

要解决此问题,您需要删除 <form /> 元素(假设它在您的逻辑中似乎没有任何作用)或在 preventDefault() 时引发的点击事件上调用 .btn-start被点击。

,

您可以简单地使用 JQuery 隐藏 功能来做到这一点:

$(".btn-start").click(function(){
    $(".winning-message").hide();
});

如果你想要动画,你也可以使用fadeOut功能。

,

你可以使用这个js代码。

$(document).ready(function(){
    
    $(".btn-start").click(function(){
        $(".winning-message").css('display','none');
    });

});

c# – 扩展方法不适用于子类?

c# – 扩展方法不适用于子类?

显然,扩展方法不适用于子类,还是仅仅是我?

private class Parent
{        
}

private class Child
{
}

public static class Extensions
{
    public static void Method(this Parent parent)
    {
    }
}

//Test code
var p = new Parent();
p.Method();            // <--- compiler like
var c = new Child();
c.Method();            // <--- compiler no like

UPDATE

在这个问题上有一个拼写错误(我要离开,以便其余的有意义) – 我忘了让Child从Parent继承.

碰巧,我真正的问题是我没有适当的使用声明.

(不幸的是,我无法删除,因为答案太多了.)

解决方法

这应该可以正常工作(LINQ扩展构建在IEnumerable< T>之上,并且它们可以在List< T>等等上工作).问题是Child在您的示例中不从Parent继承.

c# – 扩展方法不适用于接口

c# – 扩展方法不适用于接口

受MVC店面的启发,我正在研究的最新项目是使用IQueryable上的扩展方法来过滤结果.

我有这个界面;

IPrimaryKey
{
  int ID { get; }
}

我有这种扩展方法

public static IPrimaryKey GetByID(this IQueryable<IPrimaryKey> source,int id)
{
    return source(obj => obj.ID == id);
}

假设我有一个实现IPrimaryKey的类SimpleObj.当我有一个SimpleObj的IQueryable时,GetByID方法不存在,除非我明确地转换为IPrimaryKey的IQueryable,这不太理想.

我在这里错过了什么吗?

解决方法

如果做得好,它可以工作. cfeduke的解决方案有效.但是,您不必使IPrimaryKey接口通用,事实上,您根本不必更改原始定义:
public static IPrimaryKey GetByID<T>(this IQueryable<T> source,int id) where T : IPrimaryKey
{
    return source(obj => obj.ID == id);
}

关于Java 中的 split() 方法不适用于点 (.)java中split方法的作用的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于'contains' 方法不适用于 ArrayList,还有其他方法吗?、.style.display="none" 不适用于点击功能、c# – 扩展方法不适用于子类?、c# – 扩展方法不适用于接口的相关知识,请在本站寻找。

本文标签:

上一篇在 Swift 中根据 String 计算 UILabel 的大小(swift获取字符串长度)

下一篇使用 pgadmin 连接到 heroku 数据库(pgadmin怎么连接数据库)