从Java中的数组拉不同的值 [英] Pulling distinct values from a array in java

查看:105
本文介绍了从Java中的数组拉不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个程序,其中用户输入10 INT值到阵列。最后我要拉出不同的值并显示出来。增加了我的第二个循环这将确定的值是不同的(即意味着如果该号码多次出现它只显示一次)。

有关例如,让我们说,我通过在数字:1,2,3,2,1,6,3,4,5,2鲜明阵列应该只包含数字{1,2,3,6,4 ,5}

 进口java.util.Scanner中;
进口java.io. *;公共类ArrayDistinct {
 公共静态无效的主要(字串[] args)抛出IOException 扫描仪输入=新的扫描仪(System.in); //创建阵列和放大器;变量
 INT arrayLength = 10;
 INT []号=新INT [arrayLength]
 INT [] = distinctArray新INT [arrayLength]
 诠释计数= 0; 的System.out.println(程序开始......);
 System.out.print(+ numbers.length +请输入数字:); 的for(int i = 0; I< numbers.length;我++){
  号码[I] = input.nextInt();
 } 的for(int i = 0; I< numbers.length;我++){
  INT TEMP =号[I]
  INT tempTwo =号第[i + 1];  如果(tempTwo ==临时){
   算上++;
   distinctArray [I] =温度;
  }
 } //打印出结果} //主到底
} //结束类


解决方案

试试这个code ..它会工作

 包练习;
进口java.util.Scanner中;
公共类E5Second
{
    公共静态无效的主要(字串[] args)
    {
        扫描仪在=新的扫描仪(System.in);
        INT []号= INT新[10];
        fillArr(数);        布尔[] =不同的新的布尔[10];        诠释计数= 0;
        的for(int i = 0; I< number.length;我++)
        {
            如果(isThere(电话号码,我)== FALSE)
            {
                不同的[I] =真;
                算上++;
            }
        }
        的System.out.println(不同数字的\\ n此号码是+计数);
        System.out.print(以下简称不同的数字:);
        displayDistinct(数字,不同);
    }
    公共静态无效fillArr(INT []号)
    {
        扫描仪在=新的扫描仪(System.in);
        System.out.print(输入10的整数);
        的for(int i = 0; I< number.length;我++)
            号码[I] = In.nextInt();
    }
    公共静态布尔isThere(INT []号,int i)以
    {
        对于(INT J = 0; J< I; J ++)
            如果(编号[I] ==号[J]。)
                返回true;
        返回false;
    }
    公共静态无效displayDistinct(INT []号,布尔[]不同)
    {
        的for(int i = 0; I< distinct.length;我++)
            如果(不同的[I])
                System.out.print(数[I] +);
    }
}

Have a program where the user inputs 10 int values into the array. Lastly I need to pull out the distinct values and display them. Added my second for loop which would determine if the the value is distinct (i.e. meaning if the number appears multiple times it is only displayed once).

For instance, let say I pass in the numbers: 1, 2, 3, 2, 1, 6, 3, 4, 5, 2 the distinct array should only contain numbers {1, 2, 3, 6, 4, 5}

import java.util.Scanner;
import java.io.*;

public class ArrayDistinct {
 public static void main(String[] args) throws IOException {

 Scanner input = new Scanner(System.in);

 // Create arrays & variables  
 int arrayLength = 10;
 int[] numbers = new int[arrayLength];
 int[] distinctArray = new int[arrayLength];
 int count = 0;

 System.out.println("Program starting...");
 System.out.print("Please enter in " + numbers.length + " numbers: ");

 for (int i = 0; i < numbers.length; i++) {
  numbers[i] = input.nextInt();
 }

 for (int i = 0; i < numbers.length; i++) {
  int temp = numbers[i];
  int tempTwo = numbers[i + 1];

  if (tempTwo == temp) {
   count++;
   distinctArray[i] = temp;
  }
 } 

 // Print out results

} // end main
} // end class

解决方案

Try this code.. it will work

package Exercises;
import java.util.Scanner;
public class E5Second
{
    public static void main(String[] args) 
    {
        Scanner In = new Scanner(System.in);
        int [] number = new int [10];
        fillArr(number);

        boolean [] distinct = new boolean [10];

        int count = 0; 
        for (int i = 0; i < number.length; i++) 
        {
            if (isThere(number,i) == false)
            {
                distinct[i] = true;
                count++;
            }
        }
        System.out.println("\nThe number of distinct numbers is  " + count);
        System.out.print("The distinct numbers are: ");
        displayDistinct(number, distinct);
    }
    public static void fillArr(int [] number)
    {
        Scanner In = new Scanner(System.in);
        System.out.print("Enter ten integers ");
        for (int i = 0; i < number.length; i++)
            number[i] = In.nextInt();
    }
    public static boolean isThere(int [] number, int i)
    {
        for (int j = 0; j < i; j++)
            if(number[i] == number[j])
                return true;
        return false;
    }
    public static void  displayDistinct(int [] number, boolean [] distinct)
    {
        for (int i = 0; i < distinct.length; i++)
            if (distinct[i]) 
                System.out.print(number[i] + " ");
    }
}

这篇关于从Java中的数组拉不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆