小编dgh*_*htr的帖子

从列表中删除重复的元素

我开发了一个数组列表.

ArrayList<String> list = new ArrayList<String>();

list.add("1");
list.add("2");
list.add("3");
list.add("3");
list.add("5");
list.add("6");
list.add("7");
list.add("7");
list.add("1");
list.add("10");
list.add("2");
list.add("12");
Run Code Online (Sandbox Code Playgroud)

但如上所述它包含许多重复元素.我想删除该列表中的所有重复项.为此我想首先我需要将列表转换为集合.

Java是否提供将列表转换为集合的功能?是否有其他设施可以从列表中删除重复项?

java list duplicates

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

通过annomyous内部类创建线程

我正在开发创建线程的代码,但没有扩展线程类或实现runnable接口,即通过匿名内部类.

public class Mythread3 {
    public static void main(String... a) {

        Thread th = new Thread() {

            public synchronized void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(1000);

                        System.out.print(i + "\n" + "..");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

        };

        th.start();
        Thread y = new Thread();
        y.start();

    }
}
Run Code Online (Sandbox Code Playgroud)

现在请告诉我,我也可以用同样的方法创建子线程.!! 我试过的是......

public class Mythread3 {
    public static void main(String... a) {

        Thread th = new Thread() {

            public synchronized void …
Run Code Online (Sandbox Code Playgroud)

java

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

关于单身设计模式

我正在探索单身设计模式,我开发了一个类......

public class SingletonObject {
  private static SingletonObject ref;       
  private SingletonObject () { //private constructor
  }     
  public static synchronized SingletonObject getSingletonObject() {
    if (ref == null)
      ref = new SingletonObject();
    return ref;
  } 

  public Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException ();
  }
}
Run Code Online (Sandbox Code Playgroud)

但同步是非常昂贵的,所以我转向热切创建的实例的新设计而不是懒惰创建的实例.

public class Singleton {
  private static Singleton uniqueInstance = new Singleton();
  private Singleton() {
  }
  public static Singleton getInstance() {
    return uniqueInstance;
  }
}
Run Code Online (Sandbox Code Playgroud)

但请告诉我第二个设计如何优于以前的设计.. !!

java singleton design-patterns

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

关于长期和浮动

我正在开发以下代码

static String m(float i) {
    return "float";
}

static String m(double i) {
    return "double";
}

public static void main(String[] args) {
    int a1 = 1;
    long b1 = 2;
    System.out.print(m(a1) + "," + m(b1));
}
Run Code Online (Sandbox Code Playgroud)

两者都导致输出浮动,浮动,背后的原因请指教,如何调用双请告知非常感谢.

java

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

以最简单的方式创建和填充数组

我正在开发应用程序..

    class Wheel {
    private int size;

    Wheel(int s) {
        size = s;
    }

    void spin() {
        System.out.print(size + " inch wheel spinning, ");
    }

}

public class Bicycle {
    public static void main(String[] args) {
        Wheel[] wa = { new Wheel(15), new Wheel(17) };
        for (Wheel w : wa)
            w.spin();
    }
}
Run Code Online (Sandbox Code Playgroud)

但请告知我们如何Wheel[] wa = { new Wheel(15), new Wheel(17) };用更简单的术语表达.

java arrays

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

关于建设者

我正在开发以下代码....

class P {
    //public P(){}
    public P(int i) {

    }
}

class D extends P {
    public D(){ // default constructor must be defined in super class

    }
}

public class agf {      
    public static void main(String[] args) {

    }
}
Run Code Online (Sandbox Code Playgroud)

现在在类p中定义了显式参数化构造函数并在类D中定义了默认构造函数,但它仍然显示编译时错误,请解释

java

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

更有效的方法从数组列表中删除元素

我开发了类似这样的数组列表

ArrayList<String> list = new ArrayList<String>();
list.add("1");
list.add("8");
list.add("8");
list.add("3");
list.add("4");
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:如果我想从列表中删除"8",哪种方式更好?

第一种方式:

for(int i = 0; i < list.size(); i++) {
    if(list.get(i).equals("8")) {
        list.remove(i);
        i--;
    }
}
Run Code Online (Sandbox Code Playgroud)

第二种方式:

Iterator<String> iterator = list.iterator();
    while(iterator.hasNext())
        if(iterator.next().equals("8"))
            iterator.remove();
Run Code Online (Sandbox Code Playgroud)

现在请告知从性能的角度来看哪一个更高效,更快,还有其他任何类似内置功能的方法,我们可以删除重复而不需要迭代那么多.

java arraylist

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

Java Map实现

我是Java Collections的新手.我正在浏览Map,所以请告诉我就像java提供了Map一样,我们也可以制作自己的Map吗?让我们说一个名为的地图MineMap.请告诉我如何实现这一目标.我正在谷歌搜索,我发现了这样的事情:

public interface MyMap
{
    public void put(Object key,Object value);
    public Object get(Object key);
    public int size();
    public Set keySet();
    public Set entrySet();
    public interface MyEntry
    {
        public Object getKey();
        public Object getValue();
    }
}
Run Code Online (Sandbox Code Playgroud)

及其实施:

class MySimpleMap implements MyMap
{
    private ArrayList keys;
    private ArrayList values;
    private int index;

    public MySimpleMap()
    {
        keys=new ArrayList();
        values=new ArrayList();
        index=0;
    }  

    public void put(Object key,Object value)
    {
        keys.add(key);
        values.add(value);
        index++;
    }

    public Object get(Object key)
    {
        int i=keys.indexOf(key);
        if …
Run Code Online (Sandbox Code Playgroud)

java

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

关于等待和通知

我正在开发与wait¬ify相关的简单代码我创建了两个seprate类,下面是类

    class ThreadA {
 public static void main(String [] args) {
 Thread b = new Thread();
 b.start();

 synchronized(b) {
 try {
 System.out.println("Waiting for b to complete...");
 b.wait();
} catch (InterruptedException e) {}
 //System.out.println("Total is: " + b.totals);
 }
}
}

and the other one is ...





class ThreadB extends Thread {
public  int totals;

 public void run() {
     synchronized(this) {
     for(int i=0;i<100;i++) {
      totals += i;
      }
      notify();
      }
      }
     }
Run Code Online (Sandbox Code Playgroud)

但是在ThreadA类中,当我从b线程对象访问总计时,我得到了complie时间错误.

System.out.println("Total is: " + b.totals);
Run Code Online (Sandbox Code Playgroud)

请告诉我如何纠正它,以便我可以执行我的代码.. !! 在此先感谢..!1

java

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

关于StringBuffer和StringBuilder中没有重写方法?

hashCode()equals()方法均未覆盖StringBufferStringBuilder,我是想这幅下面的代码..

    //StringBuffer does not override equals & hashCode
        //StringBuffer s = new StringBuffer("saral");
        //StringBuffer s1 = new StringBuffer("saral");

        StringBuilder s = new StringBuilder("saral");
        StringBuilder s1 = new StringBuilder("saral");          

        //String s = new String("saral");
        //String s1 = new String("saral");

       HashSet set=new HashSet();
        set.add(s);
        set.add(s1);
        set.add(null);
       System.out.println("There are "+set.size()+" elements in the set.");
Run Code Online (Sandbox Code Playgroud)

从而导致在来我是个得到的是3两的情况下,当我使用StringBufferStringBuilder字符串的情况下,但2,因为字符串具有覆盖hashCode()equals()方法,请告知.

java

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

关于打破案件

我正在浏览一个switch条件语句的代码

char c1=65;
switch(c1){
  case 'A':
    System.out.println("one");
  default:
    System.out.println("two");
  case 'b':
    System.out.println("three");
}
Run Code Online (Sandbox Code Playgroud)

虽然结果是"一二三",但在调试时我发现它首先进入案例A,这是A的ASCII值65,但是它也会执行所有剩余的案例,但是如果我放弃了; 然后它出来了,所以这意味着如果我们不放弃它将继续执行所有情况请告知.

java

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

关于静态方法覆盖

在静态方法覆盖的情况下..我已经开发了以下代码

class Ab {  
    static void getF() {
        System.out.println("I am saral");
    }
}

class Ham extends Ab {
    static void getF() {
        System.out.println("I am saral saxena");
    }
    public static void main(String[] args) {
        // Ham h = new Ham();
        // h.getF(); //Ham
        Ab a = new Ham();
        a.getF(); // Ab class
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我的查询是,当我使用多态行为时,Ab a = new Ham();在静态方法覆盖的情况下,在这个阶段我仍然得到getF();类的方法Ab,请指教.

java static-methods overriding

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

关于Switch条件语句

我有下面的代码,我已经开发了..

public byte determineCardType(final IInput inputData) {
    byte cardType = UNKNOWN_CARD;
    try {
        if (isWagRewardsLoyaltyCard(inputData))
            cardType = WAG_LOYALTY_CARD_TYPE;
        else if (isDRCard(inputData))   //checking that card scanned and swiped is a DR Card
            cardType = DR_CARD_TYPE;      
        else if (isWagRewardsPartnerCard(inputData))
            cardType = AARP_CARD_TYPE;
        return cardType;
    } catch (Exception e) {
        return UNKNOWN_CARD;
    }
}
Run Code Online (Sandbox Code Playgroud)

请指教我可以在开关循环中调整上面的代码,如果是,请提前告知,提前致谢.

链接到它的其他代码......

if((aarpCardSupport.isAARPCard(input))||(determineCardType(input)==DR_CARD_TYPE)) {
    return true;
} else if((isDRCard(input))&&(isDRLoayltyEnabled())) { //would return 1 for DR card only when isDRLoayltyEnabled returns true 
    return true;
}       
return false ;
Run Code Online (Sandbox Code Playgroud)

java

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