相关疑难解决方法(0)

Java:解释for()循环参数的作用

有人可以解释一下这个类中的for循环吗?特别是(String person:people)的部分

import java.util.Scanner;

/**
 * This program uses the startsWith method to search using
 * a partial string
 * 
 *
 */
public class PersonSearch {

public static void main(String[] args){
    String lookUp;  //To hold a lookup string

    //Create an array of names
    String[] people= {"Cutshaw, Will", "Davis, George",
            "Davis, Jenny", "Russert, Phil", 
            "Russel, Cindy", "Setzer, Charles",
            "Smathers, Holly", "Smith, Chris", 
            "Smith, Brad", "Williams, Jean" };

    //Create a Scanner object for keyboard input
    Scanner keyboard=new Scanner(System.in);

    //Get a partial name …
Run Code Online (Sandbox Code Playgroud)

java

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

对于Java中的循环不起作用

import java.util.Random;
public class RandomHomework
{
    public static void main(String[] args)
    {
        int i;
        Random generator = new Random();
        double randomDecimals = generator.nextDouble()-.04;
        int randomNumber = generator.nextInt(9)+10;
        for(i = 0; i > 100; i++)
        {
            if(randomNumber >= 10.00)
            {
                System.out.println(randomNumber + randomDecimals);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的for循环设置有问题,无法解决它...当我删除for循环时,它运行得非常好.

正如你所看到的,我曾尝试过声明我,但它没有任何区别.

java random for-loop

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

为什么我可以在Java中的forEach循环中迭代数组?

通常的想法是,如果这个结构不是Iterable,我就不能用forEach迭代一些结构.但为什么我可以在Java中迭代数组呢?数组不是Iterable.

MyClass[] array = {new MyClass("a"), new MyClass("b")};

for (MyClass c : array) {
    System.out.println(c);
}
Run Code Online (Sandbox Code Playgroud)

java iterator

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

将字符串解析为JSON

我正在用org.json中的lib解析这个JSON字符串,我无法理解为什么我将下面的输出放到日志中.

ArrayList<String> al = new ArrayList<String>();
JSONObject demo = new JSONObject("{\"00408C88A2E6\":{\"id\":\"00408C88A2E6\",\"name\":\"Lab\"},\"00408C91188B\":{\"id\":\"00408C91188B\",\"name\":\"Lab1\"},\"00408C944B99\":{\"id\":\"00408C944B99\",\"name\":\"Lato1\"},\"00408C944BA0\":{\"id\":\"00408C944BA0\",\"name\":\"Lato\"}}");
Iterator<String> iterator =  demo.keys();
while (iterator.hasNext() ){    
  al.add((String)iterator.next());
  Log.i(LOG_TAG, "size al into while " + al.size());
  Log.i(LOG_TAG, "MAC " + iterator.next() + " for the user " + userId);
} 
Run Code Online (Sandbox Code Playgroud)

记录输出

07-12 08:55:34.056: INFO/parse(285): size al into while 1
07-12 08:55:34.056: INFO/parse(285): MAC 00408C91188B for the user nweb
07-12 08:55:34.066: INFO/parse(285): size al into while 2
07-12 08:55:34.066: INFO/parse(285): MAC 00408C944B99 for the user nweb
07-12 08:55:34.066: INFO/parse(285): size …
Run Code Online (Sandbox Code Playgroud)

java parsing json iterator loops

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

空指针错误有帮助吗?

嗨我得到一个空指针在过程中标记.测试者类位于底部.当试图打印动物园时会发生这种情况.似乎没有为"动物动物"设置名称,但我创造了动物.

它可以访问我想要的错误动物,但是我如何访问我已经制作的列表中的动物(请不要使用for参数中的":")!我知道这是错的,但像animal_list.printdetails?

 public class Zoo { 
    private Animal animal;  
    public int number_animals;//# animals allowed in zoo
    private List<Animal> animal_list;
    private String zoo_name;


public Zoo(String name){
    this.zoo_name = name;
    animal_list = new ArrayList<Animal>();
}

 public void addAnimal(Animal obj) {
        animal_list.add(obj);
 }
 public String toString(){
    String s = "In zoo " + zoo_name + " there are the following animals:\n";
    for (int i = 0; i < animal_list.size(); i++){
        s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";//null …
Run Code Online (Sandbox Code Playgroud)

java methods class list nullpointerexception

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

ConcurrentModificationException Minigame

你好.

我正在运行服务器并添加了一个迷你游戏.

每当游戏即将开始时......它首先调用onStart().现在我在这一行得到ConcurrentModificationException:

for(Player p : waiting) {
Run Code Online (Sandbox Code Playgroud)

这是方法:

    public void onStart() {
    trawler.players.clear();
    for(Player p : waiting) {
        if(!boat.playerInArea(p)) {
            waiting.remove(p);
        }
    }

    for(Player p : waiting) {
        trawler.players.add(p);
    }

    trawler.start();
    waiting.clear();
}
Run Code Online (Sandbox Code Playgroud)

如果你需要课程,这里是:

TrawlerWaitingRoom.java:

package server.model.minigames.trawler;

import server.model.players.Location;
import server.model.players.Player;

public class TrawlerWaitingRoom extends WaitingRoom {
private Trawler trawler;
//private Location boat = new Location(2668,2674,3165,3185);
private Location boat = new Location(2808, 2811,3415,3425);


public TrawlerWaitingRoom(Trawler trawler) {
    super(1, 2);
    this.trawler = trawler;
}

@Override
public boolean startGame() {
    if(trawler.inProgress()) …
Run Code Online (Sandbox Code Playgroud)

java exception java.util.concurrent concurrentmodification

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

ArrayList空指针异常

我创建了一个arraylist和一个ListView.我打算遍历ListView,检查它们是否被检查,然后将对象(使用ListView.getItem())添加到我的arraylist.但是我得到一个NullPointerException.ArrayList people_selected,在类顶部声明如下:

ArrayList<PeopleDetails> selected_people;
Run Code Online (Sandbox Code Playgroud)

我的代码:

for (int i = 0; i < people_list.getCount(); i++) {
              item_view = people_list.getAdapter().getView(i, null, null);
                chBox = (CheckBox) item_view.findViewById(R.id.checkBox);//your xml id value for checkBox.
                if (chBox.isChecked()) {
                    selected_people.add((PeopleDetails) people_list.getItemAtPosition(i));
                }
            }

        for(PeopleDetails person : selected_people){

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(person.number, null, sms_message, null, null);        
            }
        ///and now need to write party to file.
Run Code Online (Sandbox Code Playgroud)

我在线上得到一个错误

for(PeopleDetails person:selected_people)

说"NullPointerException".我认为这意味着arraylist是null,并且无法弄清楚为什么它应该为null.我是否在课堂上声明错了?或者我的选择和添加方法有问题?

java android listview arraylist nullpointerexception

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

我应该使用Iterator还是forloop进行迭代?

我不喜欢打电话的想法hasNext()next(),看到我是多么需要一个循环,反正调用这些,我真的不觉得用在列表和地图给我们的迭代多点.

是否只需要从列表中提取一个?或者,如果您从列表中选择一个项目,并且必须在不可测量的时间内执行此操作?我在Google或Stack上找不到这个,请帮帮我.

我不是专门讨论增强的for循环(for-each循环).

我也理解foreach循环在性能方面表现优异,但这更像是"为什么存在"的问题

编辑:已实现我只谈论集合而不是数组aswel.Foreach循环在这种情况下没有限制.

java iterator loops list map

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

使用字符串ArrayList作为参数编写方法

我正在尝试编写一个方法,该方法将字符串的ArrayList作为参数,并在每个长度为4的字符串前面放置一个包含四个星号的字符串.

但是,在我的代码中,我在构造方法时遇到错误.

这是我的标记长度类

import java.util.ArrayList;


public class Marklength {

    void marklength4(ArrayList <String> themarklength){
        for(String n : themarklength){
            if(n.length() ==4){
                themarklength.add("****");
            }
        }
        System.out.println(themarklength);
    }

}
Run Code Online (Sandbox Code Playgroud)

以下是我的主要课程:

import java.util.ArrayList;


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

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

        words.add("Kane");
        words.add("Cane");
        words.add("Fame");
        words.add("Dame");
        words.add("Lame");  
        words.add("Same");

        Marklength ish = new Marklength();

        ish.marklength4(words);

    }
}
Run Code Online (Sandbox Code Playgroud)

基本上在这种情况下,它应该运行,因此它添加一个arraylist,其中一个字符串"****"放在数组列表的每个前一个元素之前,因为字符串的长度都是4.

BTW

这包括添加另一个元素

我不知道我哪里出错了.可能在我的for循环中?

我收到以下错误:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
    at java.util.AbstractList$Itr.next(AbstractList.java:343)
    at Marklength.marklength4(Marklength.java:7)
    at MarklengthTestDrive.main(MarklengthTestDrive.java:18)
Run Code Online (Sandbox Code Playgroud)

非常感谢你.感谢帮助.

java methods arraylist

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

FindBugs警告:使用keySet迭代器效率低下

这是一个类似的问题[ FindBugs警告:使用keySet迭代器而不是entrySet迭代器的效率低下

但是,我想尝试做一些不同的事情.我目前的代码在这里:

for (Double key2 : sortedPolygons.keySet()) {
    if (sortedPolygons.get(key2).getExteriorRing().equals(hole)) {
        sortedPolygons.remove(key2);
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

在链接中执行类似解决方案的操作不起作用.以下是所述解决方案的实现:

for(Map.Entry<Double, Polygon> entry : sortedPolygons.entrySet()) {
    if (entry.getValue().getExteriorRing().equals(hole)) {
         .....
Run Code Online (Sandbox Code Playgroud)

这里的问题是我试图删除该条目.没有entry.remove().如何在没有FindBugs错误的情况下替换我的第一个代码块:

低效使用keySet迭代器而不是entrySet迭代器 - >

此方法使用从keySet迭代器检索的键访问Map条目的值.在map的entrySet上使用迭代器更有效,以避免Map.get(键)查找.

需要注意的是TreeMap,底层结构是,并且无法更改.

java iterator findbugs treemap sonarqube

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