logo
Tags down

shadow

Fill an array from keyboard in Java


By : Neil
Date : October 18 2020, 01:08 AM
I think the issue was by ths following ,
I don't know how to configure the Scanner to fill an array/ArrayList. How could i do?
code :
//example, prompt user 3 times
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i<3; i++)
    list.add(scn.nextInt());
array[i] = scn.nextInt();


Share : facebook icon twitter icon

How to fill multidimensional array of specific objects in Java with fill() method?


By : tuffyguy
Date : March 29 2020, 07:55 AM
I wish this help you Answer to your question: Yes, there is a difference (see JavaDoc).
Your first version puts one object instance into every single array element of a row. So a change to this instance is visible in every element in the same row of the array. You'll have i ClassB instances in total.
code :
for (ClassB[] classB_1Array : classB_2Array) {
    ClassB instance = new ClassB();
    Arrays.fill(classB_1Array, instance);
}
import java.util.Arrays;

public class ArrayFiller {
    public static void main(String[] args) {
        // your first version:
        Person[][] yourFirstVersion = new Person[2][2];
        for (Person[] array : yourFirstVersion) {
            Arrays.fill(array, new Person("Mike"));
        }
        System.out.println(Arrays.deepToString(yourFirstVersion));
        yourFirstVersion[0][1].setName("Paul");
        System.out.println(Arrays.deepToString(yourFirstVersion));
        System.out.println("-----");
        // equivalent: my version:
        Person[][] myVersion = new Person[2][2];
        for (Person[] array : myVersion) {
            Person person = new Person("John");
            Arrays.fill(array, person);
        }
        System.out.println(Arrays.deepToString(myVersion));
        myVersion[0][1].setName("Thomas");
        System.out.println(Arrays.deepToString(myVersion));
        System.out.println("-----");
        // your second version
        Person[][] yourSecondVersion = new Person[2][2];
        for (int i = 0; i < yourSecondVersion.length; i++) {
            for (int j = 0; j < yourSecondVersion[i].length; j++) {
                yourSecondVersion[i][j] = new Person("Max");
            }
        }
        System.out.println(Arrays.deepToString(yourSecondVersion));
        yourSecondVersion[0][1].setName("Chris");
        System.out.println(Arrays.deepToString(yourSecondVersion));
    }

    private static class Person {
        private String name;
        public Person(String name) {
            System.out.println("Constructor called for " + name);
            this.name = name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return name;
        }
    }
}
Constructor called for Mike
Constructor called for Mike
[[Mike, Mike], [Mike, Mike]]
[[Paul, Paul], [Mike, Mike]]
-----
Constructor called for John
Constructor called for John
[[John, John], [John, John]]
[[Thomas, Thomas], [John, John]]
-----
Constructor called for Max
Constructor called for Max
Constructor called for Max
Constructor called for Max
[[Max, Max], [Max, Max]]
[[Max, Chris], [Max, Max]]

Fill in an array from a single formatted keyboard line in C


By : Mannai Sourour
Date : March 29 2020, 07:55 AM
I wish this helpful for you
I know that I can use scanf as follows, but that does not solve my problem, since each element should be entered one by one.
code :
char buf[LINE_MAX];
fgets(buf, sizeof buf, stdin);

char *s = buf, *endp;
int i = 0;
for (i = 0; i < 13; i++) {
    array[i] = strtof(s, &endp);
    s = endp;
}

Fill an new array with elements of array A that are missing form array B - Java


By : Maitiú Ó Ciarain
Date : March 29 2020, 07:55 AM
I wish this helpful for you I suggest you to use one temporary array (say listC) to store the positions of the elements that we need to copy.
Iterating through both arrays for the first time, you calculate the size of the new array and set flags if the elements are unique.
code :
int[] listA = {2, -5, -121, 102, -35, -2, 0, -125, 802, -10, 7, 555};
int[] listB = {6, 99, -1, 12, 1, -2, 7, 555};

// array of flags indicating which elements of listA to copy
int[] listC = new int[listA.length];

// counter of unique elements from listA
int counter = 0;

for (int i = 0; i < listA.length; i++) {
    boolean contains = false;
    //simplified form of for loop if you don't use an index
    for (int b: listB) {
        if (listA[i] == b) {
            contains = true;
            break;
        }
    }
    if (!contains) {
        counter++;
        //setting listC[i] to 1 if listA[i] is not present in listB
        listC[i] = 1;
    }
}

// declaring array with specified size 
int[] listD = new int[counter];

counter = 0;

// iterating through the array of flags to copy unique elements
for (int i = 0; i < listC.length; i++) {
    if (listC[i] == 1) {
        listD[counter++] = listA[i];
    }
}

System.out.println(Arrays.toString(listD));

Java List: When I call Arrays.fill() to fill an array, why it will appear some unexpected elements?


By : user1812004
Date : March 29 2020, 07:55 AM
I wish this help you I am a beginner programmer, and it's my first time to ask a question here, if there is any worry about the format, I will feel really sorry. And at first, thank you for your answer, for your patience, and your pardon. , The toIndex parameter of Arrays.fill is exclusive, so change:
code :
Arrays.fill(cardNo, i*13, (i+1)*13 - 1, i+1);
Arrays.fill(cardNo, i*13, (i+1)*13, i+1);

Dynamic array fill from keyboard input[C]


By : heskethm
Date : March 29 2020, 07:55 AM
hope this fix your issue Something like this could work.
Only one pointer is needed for getline. Since the pointer is assigned array->memory[++array->index] = data; more memory needs to be allocated for each iteration. Set line to NULL and len to zero.
code :
char *line = NULL;
size_t len = 0;
ssize_t read;
puts("Enter your first ARRAY : ");

struct dynarray *first;
create_dynarray(&first, 1);

while((read = getline(&line, &len, stdin)) != -1){
    if ( '\n' == line[0]) {
        break;
    }
    if(read > 0){
        add_elem(first, line);
        line = NULL;
        len = 0;
    }
}

puts("Enter your second ARRAY :");

struct dynarray *second;
create_dynarray(&second, 1);

while((read = getline(&line, &len, stdin)) != -1){
    if ( '\n' == line[0]) {
        break;
    }
    if(read > 0){
        add_elem(second, line);
        line = NULL;
        len = 0;

    }
}
free(line);
shadow
Privacy Policy - Terms - Contact Us © soohba.com