0

The question is: get a score of 10 peoples; then you need to ask each one with a score higher than 80 if he would want to continue studying 'y' for yes and 'n' for no. Next you need to get their location in the array so if person number 5 array[5]=90 and answers 'y' it will make a new array with newarray[1]=[5(his location)]

My question is how to define the newarray without knowing its length(how many 'y' there would be). EDIT:

import java.util.Scanner;
public class p44_52 {
static Scanner reader = new Scanner(System.in);
static void input(int []a)
{
    for(int i=0; i<a.length;i++){
        System.out.println("Write an score of a student");
        a[i]= reader.nextInt();
    }
}
    

static void output(int []b)
{
    for (int i =0; i<b.length;i++)
    {
        System.out.println("serial number of who choose to continue and score above 80  "+ b[i]);
    }
}

public static void main(String[]args){
    
    char c = 'g';
    int count =0;
    int []score = new int[10];
    kelet(score);

    for(int i=0; i<score.length;i++)
    {
        if(score[i]>80)
        {
            System.out.println("Studnet number "+i+ " Do you want to continue?");
            c = reader.next().charAt(0);
            if(c == 'y'){
                //Here i want to make the new array(length isn't known) for their locationnumbers
            }
        }
    }
    
    
}

}

6
  • it is impossible. you have to know the length for sure Commented Dec 24, 2014 at 19:56
  • Can't you just use 10? Commented Dec 24, 2014 at 19:58
  • possible duplicate of Variable length (Dynamic) Arrays in Java Commented Dec 24, 2014 at 20:04
  • Do you have to use arrays? The collections in java.util (ArrayList, Set, Map) were created to solve just the sorts of problems you're running into, since their capacity can increase dynamically. But if your professor wants you to only use arrays, then they're not an option... Commented Dec 24, 2014 at 20:07
  • @SufiyanGhori, this is a duplicate of the post you refereced only if the homework assignment allows the use of the collections in java.util. If not, then that post doesn't answer this question... Commented Dec 24, 2014 at 20:08

5 Answers 5

2

You can create a temporary array with the same size as the full list (because you know that's the largest it could need to be) and populate it with as many students as choose to continue. There will probably be fewer elements actually in the temporary array than it can hold because some students won't continue, so then you can create a new array that's the right size (now that you know it) and use System.arraycopy() to copy all of the elements into the right-size array.

It's not the most efficient way to do it, but it's not terrible (it just uses one extra array allocation and copy operation) and it's certainly good enough for homework. And it doesn't use anything other than arrays.

In general, this is a technique you'll use from time to time as you write programs: if there's something you can't do until you've done some operation, then look for a way to break the operation into multiple steps so you can rearrange steps in an order that is possible but still yields the same result. Better still is to find a different data structure that meets your needs (as ArrayList will, since its capacity can be grown at runtime), but there will be times when you can't just drop in a different data structure, and this approach of breaking the problem down and rearranging steps can be useful then.

Sign up to request clarification or add additional context in comments.

Comments

1

in java you can use ArrayList instead of Array if you don't want to give initial capacity..

List<Integer> list= new ArrayList<Integer>(); 
list.add(10); //you can add elements dynmically.

//to get data you can use 
list.get(0); //index at which you want data

1 Comment

Can you write the code of how to use that? how to input numbers to it and how to output from it?
0

Depends on the language your programming in. In java have to declare the amount of space in the array before you can use it. This is because Java sets aside the space for the array. Other languages such as Python dont require you to declare the amount of space used. There are ways of getting around this in Java like using the arraylist.

4 Comments

Python's isn't an array, it's a list, which is a different concept. Java's ArrayList is a list based on an array, not an array.
@AlexM. I totally agree with you. I am confused how this answer helps the op since the op's question is very unclear to me at least
@AlexM. lists in Pythons are implemented as arrays. It totally is the same thing as a Java ArrayList<T>.
@rightføld, I think Alex M. was saying that both of those things are lists (which are different from the arrays the OP was asking about) that are built on arrays, not that the two implementations of lists are somehow fundamentally different. He's saying the same thing you are.
0

ArrayLists

Arraylists were created to solve the length issue of arrays.

in your example:

for(int i=0; i<score.length;i++)
{
    if(score[i]>80)
    {
        System.out.println("Studnet number "+i+ " Do you want to continue?");
        c = reader.next().charAt(0);
        if(c == 'y'){
            //Here i want to make the new array(length isn't known) for their locationnumbers
        }
    }
}

you want to do something like this:

 List<String> continueStudy = new ArrayList<String>();
 for(int i=0; i<score.length;i++)
 {
    if(score[i]>80)
    {
        System.out.println("Studnet number "+i+ " Do you want to continue?");
        c = reader.next().charAt(0);
        if(c == 'y'){
            continueStudy.add(c);
        }
    }
}

Then you can use a for loop to evaluate each string such as:

for(String Value : continueStudy){
    if(value.contains('y')){
       //DO SOMETHING or call seperate function
    }
}

Comments

0

Use an arraylist. If he says no then you remove it. At the end you

yourArrayList.toArray()

to convert the arrayList to an array.

EDIT :

public static void main(String[]args){

  char c = 'g';
  int count =0;
  int []score = new int[10];
  ArrayList<Integer> list = new ArrayList<Integer>();
  kelet(score);

  for(int i=0; i<score.length;i++)
  {
    if(score[i]>80)
    {
        System.out.println("Studnet number "+i+ " Do you want to continue?");
        c = reader.next().charAt(0);
        if(c == 'y'){
            list.add(//your variable);
        }
    }
  }

  //If you want turn it into array
  int [] listArray = list.toArray();


}

1 Comment

Can you write the code of how to use that? how to input numbers to it and how to output from it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.