访问另一个类中的树状图并循环遍历项目。

huangapple go评论129阅读模式
英文:

Access treemap in another class and loop through the items

问题

这是我的第一个Java类,其中包括我想要循环遍历的TreeMap:

package myFunctions;

import java.util.TreeMap;

public class myUrls {

    public void main() {

        TreeMap<String, String> getUrl = new TreeMap<String, String>();

        getUrl.put("app1", "URL 1");

        getUrl.put("app2", "URL 2");

    }

    // 打印键和值
    for (String i : getUrl.keySet()) {
        System.out.println("app name: " + i + " url: " + getUrl.get(i));
    }

}

这是我的第二个类,在这个类中,我想要获取之前提到的TreeMap并对其进行循环遍历:

package myFunctions;

public anotherClass() {

    DA_devurl myUrls = new DA_devurl();

    // 循环遍历数组并针对每个键值对执行不同的操作

}
英文:

Here's my first Java class, which includes the TreeMap that I want to loop through:

package myFunctions;

import java.util.TreeMap;

public class myUrls {

	public void  main() {
	
	    TreeMap&lt;String, String&gt; getUrl = new TreeMap&lt;String, String&gt;();
		
	    getUrl.put(&quot;app1&quot;, &quot;URL 1&quot;);
		
	    getUrl.put(&quot;app2&quot;, &quot;URL 2&quot;);		   
	    
	  }
	
	// Print keys and values
	for (String i : getUrl.keySet()) {
	  System.out.println(&quot;app name: &quot; + i + &quot; url: &quot; + getUrl.get(i));
	 
	}
	
 }
}

Here's my second class, where I want to take the previously mentioned TreeMap and loop through it:

package myFunctions;

public anotherClass() {

	DA_devurl myUrls = new DA_devurl();	
	
	//Loop through the array and perform seperate actions for each keyvalue pair

} 

答案1

得分: 2

在你的第一个类中,我建议将你的TreeMap作为私有实例变量,并提供一个获取器方法:

package functions;

import java.util.TreeMap;

public class MyUrls {
    private TreeMap<String, String> getUrl;

    public MyUrls() {
        getUrl = new TreeMap<String, String>();
    }

    public void main() {
        getUrl.put("app1", "URL 1");
        getUrl.put("app2", "URL 2");           
    } 

    public TreeMap<String, String> getGetUrl() {
        return this.getUrl;
    }
}

现在,在你的第二个类中,你只需要在创建前一个类的实例后调用获取器方法,然后使用正确的语法循环遍历其条目:

package functions;

import java.util.TreeMap;
import java.util.Map;

public class AnotherClass {
    public static void main(String args[]) {
        MyUrls myUrl = new MyUrls(); 

        // 填充TreeMap
        myUrl.main(); 
        TreeMap<String, String> urls = myUrl.getGetUrl();

        // 循环遍历数组并对每个键值对执行单独的操作
        for (Map.Entry<String, String> entry : urls.entrySet()) {
            // 使用 entry.getKey() 和 entry.getValue() 做一些操作
        } 
    }
}
英文:

In your first class, I recommend making your TreeMap a private instance variable with a getter method:

package functions;

import java.util.TreeMap;

public class MyUrls {
    private TreeMap&lt;String, String&gt; getUrl;

    public MyUrls() {
        getUrl = new TreeMap&lt;String, String&gt;();
    }

    public void main() {
        getUrl.put(&quot;app1&quot;, &quot;URL 1&quot;);
        getUrl.put(&quot;app2&quot;, &quot;URL 2&quot;);           
    } 

    public TreeMap&lt;String, String&gt; getGetUrl() {
        return this.getUrl;
    }
}

Now, in your second class, all you need to do is call the getter method after creating an instance of your previous class, and then looping through its entries with the proper syntax:

package functions;

import java.util.TreeMap;
import java.util.Map;

public class AnotherClass {
    public static void main(String args[]) {
      MyUrls myUrl = new MyUrls(); 
    
      // populates the TreeMap
      myUrl.main(); 
      TreeMap&lt;String, String&gt; urls = myUrl.getGetUrls();
    
      // Loop through the array and perform seperate actions for each keyvalue pair
      for (Map.Entry&lt;String, String&gt; entry : urls.entrySet()) {
         // do something with entry.getKey() and entry.getValue()
      } 
   }
}

huangapple
  • 本文由 发表于 2020年8月4日 13:05:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63240557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定