小编Mar*_*ers的帖子

Multimaps中的Java Generics擦除

我有两个MultimapString通过(i)索引小号IntegerS和(ⅱ)DoubleS和一个例程到的输出列出String秒.

public static void outputInteger(Multimap<Integer, String> map) {
    for (Integer key : map.keySet()) {
        Collection<String> strings = map.get(key);
        output(strings);
    }
}

public static void outputDouble(Multimap<Double, String> map) {
    for (Double key : map.keySet()) {
        Collection<String> strings = map.get(key);
        output(strings);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想用这些组合成一个单一的程序Number为一体的超IntegerDouble

public static void outputNumber(Multimap<? extends Number, String> map) {
    for (Number key : map.keySet()) {
        Collection<String> ids = map.get(key); //** 
    }
}
Run Code Online (Sandbox Code Playgroud)

但是星号线不能编译 …

java generics guava

4
推荐指数
3
解决办法
400
查看次数

Java中的字符串操作问题

我昨天在接受采访时询问了以下2个问题

1>给定一个字符串,计算一个新字符串,其中原始字符串中相邻的相同字符之间用"*"分隔.

示例如下所示::
function name ispublic String pairStar(String str)

 pairStar("hello") ? "hel*lo"     
 pairStar("xxyy") ? "x*xy*y"          
 pairStar("aaaa") ? "a*a*a*a"
Run Code Online (Sandbox Code Playgroud)

2>给定一个字符串,计算一个新的字符串,其中所有小写的"x"字符都已移动到字符串的末尾.

示例如下所示::
function name ispublic String endX(String str)

endX("xxre") ? "rexx"     
endX("xxhixx") ? "hixxxx"     
endX("xhixhix") ? "hihixxx"
Run Code Online (Sandbox Code Playgroud)

我不知道如何完成给定的问题,并努力解决这个问题

java string

3
推荐指数
2
解决办法
8738
查看次数

如何将子类参数传递给超类私有变量?

我对如何从新对象实例中获取参数也流入超类以更新超类中的私有字段感到困惑。

所以我在一个高级Java课上,并且我的作业需要一个“ Person”超级类和一个扩展“ Person”的“ Student”子类。

Person类存储学生姓名,但它是Student类的构造函数,它接受Person名称。

假设Person中没有方法可以更新变量方法...例如subClassVar = setSuperClassVar();

例如:

public class Person
{
     private String name; //holds the name of the person
     private boolean mood; //holds the mood happy or sad for the person
     private int dollars; //holds their bank account balance
}

class Student extends Person //I also have a tutor class that will extend Person as well
{
     private String degreeMajor //holds the var for the student's major they have for their degree

     Public Student(String startName, int …
Run Code Online (Sandbox Code Playgroud)

java parameters constructor superclass

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

打印字符串中的数字(不是数字)的总和

我最近被要求在java中编写一个函数,它将对字符串中的数字(而不是数字)求和.例如,如果字符串是abc35zz400tt15,则输出应为450.

这就是我写的:

 public static void getSum(String a){
        String[] arr=a.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
        int sum=0;
        for(int i=0;i<arr.length;i++){
            if(Pattern.matches("[0-9]+", arr[i]))
        sum+=Integer.parseInt(arr[i]);
    }
    System.out.println(sum);
    }
Run Code Online (Sandbox Code Playgroud)

是否有更有效的方法来执行此操作,因为他们对上述代码不满意.

java regex string

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

如何在eclipse中运行简单的JUnit测试

所以这是我的代码:

package tests;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

class StatementTester extends TestCase { 
    public StatementTester (String name) { 
        super(name); 
    }

    protected void setUp() { 
    } 

    protected void tearDown() {  
    } 

    public void test1() {
        System.out.println("testing");
        assert(true);
    }

    public static Test suite() { 
        TestSuite suite= new TestSuite(); 
        suite.addTest(new StatementTester("test1")); 
        return suite; 
    }

    public static void main (String[] args) { 
        junit.textui.TestRunner.run (suite());
  } 
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行它时,我收到以下错误消息:

.E
Time: 0.001
There was 1 error:
1) test1(tests.StatementTester) at tests.StatementTester.main(StatementTester.java:30)

FAILURES!!!
Tests run: 1,  Failures: …
Run Code Online (Sandbox Code Playgroud)

java eclipse junit unit-testing junit3

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

为什么这个线程允​​许另一个线程访问其同步方法?

我有以下代码.我希望一个线程完全执行其synchronized方法,然后允许另一个线程访问相同的方法.然而,这种情况并非如此.

public class Threads  {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //Thread Th = new Threads();
        Thread th = new Thread (new thread1 ());
        th.start();
        Thread th1 = new Thread (new thread1 ());
        th1.start();
    }
}



class thread1 implements Runnable{
    String name = "vimal";

    public void run() {
        System.out.println("Runnable "+this.name);
        setNAme("Manish");

    }

    public synchronized void setNAme(String name){
        try {
            System.out.println("Thread "+Thread.currentThread().getName());
            wait(1000);
            this.name = name;
            System.out.println("Name "+this.name);

        } catch (InterruptedException e) {
            // TODO Auto-generated …
Run Code Online (Sandbox Code Playgroud)

java

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

虽然循环没有完成

我是Java的新手,我开始使用一些简单的控制台应用程序.

这是我当前的应用程序代码:

Scanner sc = new Scanner(System.in);
boolean ExitLoop = false;
ArrayList<Integer> IDs = new ArrayList<Integer>();
ArrayList<Double> averages = new ArrayList<Double>();
while(!ExitLoop)
{
    System.out.println("StudentID: ");
    IDs.add(sc.nextInt());
    System.out.println("Average: ");
    averages.add(sc.nextDouble());
    System.out.println("Do you want to register another student? [y/n] ");
    ExitLoop = (sc.next() == "n");
}
Run Code Online (Sandbox Code Playgroud)

很抱歉问这么愚蠢的问题,但我真的陷入了这个问题,我点击了"n",但是while循环没有停止,并继续工作.我做错了什么吗?当用户输入"n"表示否时,我该怎么做才能完成循环?

java eclipse while-loop

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

Java upcast范围解析问题

我有一个基类"Shapes"和一个扩展类"Circle".两者都有一个getName方法.我的测试类是"Driver"类.

我将Circle对象向上转换为Shapes并将其传递给名为polyTest的函数.在该函数中,我想调用getName,但我不希望触发get name的循环对象实现,而是希望触发基类实现.

super.getName()不起作用.

我的代码是吼叫.

public class Driver{
     public static String polyTest (Shapes s){
        return s.getName(); 
       /*Instead of s.getName()... (gives me the Circle class implementation of getName() )
       I want to call s.Shapes::GetName, the base class implementation of getName. */
     }

     public static void main(String[] args){
      Circle c = new Circle();

      //Test Basic inheritance & basic polymorphism.
      //System.out.print(c.getName());

      //Upcast test.
      Shapes s = (Shapes) c;
      System.out.print( polyTest(s) );    
     }
}

public class Circle extends Shapes{
     Circle(){
      super();
     }

     public String …
Run Code Online (Sandbox Code Playgroud)

java

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