【问题标题】:Get an item by index from the HashSet array and also display the average length of each item in the list [closed]从 HashSet 数组中按索引获取一个项目,并显示列表中每个项目的平均长度 [关闭]
【发布时间】:2014-10-03 19:50:09
【问题描述】:

您好,我创建了使用 HashSet 数组的代码。我是 Java 编程的新手,我想知道如何完成这两个任务:

*通过索引从HashSet数组中获取一项 *显示列表中每一项的平均长度

我的代码并不长,所以我将整个代码粘贴在这里。感谢您的所有帮助。

public class MainActivity extends Activity {

Button aButton; // Global Scope
Button sButton;
TextView text2;
EditText eText;
HashSet<String> list = new HashSet<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_layout); 

    aButton = (Button) this.findViewById(R.id.button1);
    text2 = (TextView) this.findViewById(R.id.textView1);

    //Clickable Saved Input will display alert
    text2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Complete!", Toast.LENGTH_LONG).show();

        }
    });

    aButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            list.add("Books");
            list.add("Newspapers");
            list.add("Magazines");
            String listString = "";

            for (String s : list) {
                listString += s + " - ";
            }
            text2.setText(listString);
            }
        });




    sButton = (Button) this.findViewById(R.id.button2);
    eText = (EditText) this.findViewById(R.id.editText1);

    sButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {


             //Log.v("EditText", eText.getText().toString());
            if( !list.add(eText.getText().toString()) )
            {
                System.out.println("Not Unique Item");
                Toast.makeText(getApplicationContext(), "Already Saved!", Toast.LENGTH_LONG).show();
            } else 
            {
                System.out.println("Unique Entry Added");
                Toast.makeText(getApplicationContext(), "Saved To Items.", Toast.LENGTH_LONG).show();

            }


        }

    });


    }

}

【问题讨论】:

  • 顾名思义,HashSetSet(包含不同(wrt.equals)元素无特定顺序的集合)。如果您想按特定顺序存储元素并按索引检索它们,除了HashSet 之外,您还必须维护List(例如ArrayList)。
  • 请使用stringbuilder。

标签: java arrays


【解决方案1】:

Set 不是你想要的,因为:

  • 设置只保留唯一
  • Set 的元素没有顺序,因此不能通过索引访问元素

更好的选择是 List,例如 ArrayList,它允许任何(即重复)值,并且可以使用 list.get(i) 通过索引访问。

【讨论】:

  • 如果我理解您的回答,您说的是两个项目符号中的通用集合,对吧?如果是这样,那么就有一种具有“可预测迭代顺序”的集合:LinkedHashSet。哪个仍然可以提出问题:“为什么不能直接获得 LHS 的第 N 个元素,而不遍历 N-1 个元素?” Sets 的一般约定是不保证任何类型的排序,因此不能保证直接位置访问。我同意你关于使用列表的建议。
  • @corindiano 不,方坯点适用于有类型和无类型(即原始)集。 LinkedHashSet 的合约(通过 javadoc)仅保证 iteration 顺序与 insertion 顺序相同。这是在内部使用 LinkedList 而不是 ArrayList(因此得名 LinkedHashSet)实现的,它仍然不允许通过索引直接访问元素。顺便说一句,使用 LinkedList 可以保证所有操作在恒定时间内完成;删除 ArrayList 是 O(n)。
【解决方案2】:

当您不想在集合中允许重复值时,应使用 HashSet。在 HashSet 中,您将无法通过索引找到值。如果你仍然想找到元素,你别无选择,只能使用迭代器,它会遍历 Hashset 并从 Hashset 中一个一个地给你。

你想通过索引找到你的元素,你应该使用 ArrayList。

有两种迭代HashSet的方式——

HashSet <String> newset = new HashSet <String>();

 // populate hash set
      newset.add("A"); 
      newset.add("BB");
      newset.add("CCC");  

      // create an iterator
      Iterator iterator = newset.iterator(); 

      // check values
      while (iterator.hasNext()){
         System.out.println("Value: "+iterator.next() + " ");  
      }

第二种方法是使用for循环-

for (String s : newset ) {
    System.out.println("Value: " +s);
}

【讨论】:

  • 是的,我正在寻找一个迭代器。问题是我不知道如何为它创建代码。这是一个漫长的过程吗?我尝试了谷歌的参考,但我没有找到很多教程来完成这个。你能帮我吗?
  • 有两种方式,看我的更新ans。
  • 哪种方法可以通过索引从 HashSet 数组中获取 1 项?我真的不在乎我只需要能够得到 1。有可能吗?
  • 尝试使用 ArrayList,当您在数组列表中添加项目时,您可以像定义 ArrayList aa = new ArrayList();而不是在数组列表中添加项目,而不是像 aa.get(indexvalue) 这样按索引查找项目; indexvalue 将是 0,1,2.....
  • 供您参考 - java67.blogspot.com/2012/07/…。我希望它会对您有所帮助。HashSet 和 ArrayList 之间的区别在于它基于索引的您可以通过调用 get(index) 来检索对象或通过调用 remove(index) 来删除对象,而 HashSet 完全基于对象。 HashSet 也不提供 get() 方法。
猜你喜欢
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 2023-03-17
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
相关资源
最近更新 更多